drm: Split drm_map and drm_local_map
[deliverable/linux.git] / drivers / gpu / drm / drm_bufs.c
CommitLineData
1da177e4 1/**
b5e89ed5 2 * \file drm_bufs.c
1da177e4 3 * Generic buffer template
b5e89ed5 4 *
1da177e4
LT
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
11 *
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
36#include <linux/vmalloc.h>
37#include "drmP.h"
38
d883f7f1 39resource_size_t drm_get_resource_start(struct drm_device *dev, unsigned int resource)
1da177e4 40{
836cf046
DA
41 return pci_resource_start(dev->pdev, resource);
42}
43EXPORT_SYMBOL(drm_get_resource_start);
1da177e4 44
d883f7f1 45resource_size_t drm_get_resource_len(struct drm_device *dev, unsigned int resource)
836cf046
DA
46{
47 return pci_resource_len(dev->pdev, resource);
48}
b5e89ed5 49
836cf046 50EXPORT_SYMBOL(drm_get_resource_len);
1da177e4 51
55910517 52static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
f77d390c 53 struct drm_local_map *map)
836cf046 54{
55910517 55 struct drm_map_list *entry;
bd1b331f 56 list_for_each_entry(entry, &dev->maplist, head) {
7c1c2871 57 if (entry->map && (entry->master == dev->primary->master) && (map->type == entry->map->type) &&
54ba2f76 58 ((entry->map->offset == map->offset) ||
7c1c2871 59 ((map->type == _DRM_SHM) && (map->flags&_DRM_CONTAINS_LOCK)))) {
89625eb1 60 return entry;
836cf046
DA
61 }
62 }
63
64 return NULL;
1da177e4 65}
1da177e4 66
e0be428e 67static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
fb41e54b 68 unsigned long user_token, int hashed_handle)
d1f2b55a 69{
8d153f71 70 int use_hashed_handle;
c2604ce0 71#if (BITS_PER_LONG == 64)
8d153f71
TH
72 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
73#elif (BITS_PER_LONG == 32)
74 use_hashed_handle = hashed_handle;
75#else
76#error Unsupported long size. Neither 64 nor 32 bits.
77#endif
d1f2b55a 78
e08870c8
TH
79 if (!use_hashed_handle) {
80 int ret;
1545085a 81 hash->key = user_token >> PAGE_SHIFT;
e08870c8
TH
82 ret = drm_ht_insert_item(&dev->map_hash, hash);
83 if (ret != -EINVAL)
84 return ret;
d1f2b55a 85 }
e08870c8
TH
86 return drm_ht_just_insert_please(&dev->map_hash, hash,
87 user_token, 32 - PAGE_SHIFT - 3,
1545085a 88 0, DRM_MAP_HASH_OFFSET >> PAGE_SHIFT);
d1f2b55a 89}
9a186645 90
1da177e4 91/**
f77d390c
BH
92 * Core function to create a range of memory available for mapping by a
93 * non-root process.
1da177e4
LT
94 *
95 * Adjusts the memory offset to its absolute value according to the mapping
96 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
97 * applicable and if supported by the kernel.
98 */
84b1fd10 99static int drm_addmap_core(struct drm_device * dev, unsigned int offset,
c60ce623 100 unsigned int size, enum drm_map_type type,
55910517
DA
101 enum drm_map_flags flags,
102 struct drm_map_list ** maplist)
1da177e4 103{
f77d390c 104 struct drm_local_map *map;
55910517 105 struct drm_map_list *list;
9c8da5eb 106 drm_dma_handle_t *dmah;
8d153f71
TH
107 unsigned long user_token;
108 int ret;
1da177e4 109
b5e89ed5
DA
110 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
111 if (!map)
1da177e4
LT
112 return -ENOMEM;
113
7ab98401
DA
114 map->offset = offset;
115 map->size = size;
116 map->flags = flags;
117 map->type = type;
1da177e4
LT
118
119 /* Only allow shared memory to be removable since we only keep enough
120 * book keeping information about shared memory to allow for removal
121 * when processes fork.
122 */
b5e89ed5
DA
123 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
124 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
1da177e4
LT
125 return -EINVAL;
126 }
b5e89ed5
DA
127 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
128 map->offset, map->size, map->type);
129 if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
130 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
1da177e4
LT
131 return -EINVAL;
132 }
b5e89ed5 133 map->mtrr = -1;
1da177e4
LT
134 map->handle = NULL;
135
b5e89ed5 136 switch (map->type) {
1da177e4
LT
137 case _DRM_REGISTERS:
138 case _DRM_FRAME_BUFFER:
88f399cd 139#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
8d2ea625 140 if (map->offset + (map->size-1) < map->offset ||
b5e89ed5
DA
141 map->offset < virt_to_phys(high_memory)) {
142 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
1da177e4
LT
143 return -EINVAL;
144 }
145#endif
146#ifdef __alpha__
147 map->offset += dev->hose->mem_space->start;
148#endif
836cf046
DA
149 /* Some drivers preinitialize some maps, without the X Server
150 * needing to be aware of it. Therefore, we just return success
151 * when the server tries to create a duplicate map.
152 */
89625eb1
DA
153 list = drm_find_matching_map(dev, map);
154 if (list != NULL) {
155 if (list->map->size != map->size) {
836cf046 156 DRM_DEBUG("Matching maps of type %d with "
b5e89ed5
DA
157 "mismatched sizes, (%ld vs %ld)\n",
158 map->type, map->size,
159 list->map->size);
89625eb1 160 list->map->size = map->size;
836cf046
DA
161 }
162
163 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
89625eb1 164 *maplist = list;
836cf046
DA
165 return 0;
166 }
167
1da177e4 168 if (drm_core_has_MTRR(dev)) {
b5e89ed5
DA
169 if (map->type == _DRM_FRAME_BUFFER ||
170 (map->flags & _DRM_WRITE_COMBINING)) {
171 map->mtrr = mtrr_add(map->offset, map->size,
172 MTRR_TYPE_WRCOMB, 1);
1da177e4
LT
173 }
174 }
0769d39c 175 if (map->type == _DRM_REGISTERS) {
004a7727 176 map->handle = ioremap(map->offset, map->size);
0769d39c
ST
177 if (!map->handle) {
178 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
179 return -ENOMEM;
180 }
181 }
bc5f4523 182
1da177e4 183 break;
1da177e4 184 case _DRM_SHM:
54ba2f76
DA
185 list = drm_find_matching_map(dev, map);
186 if (list != NULL) {
187 if(list->map->size != map->size) {
188 DRM_DEBUG("Matching maps of type %d with "
189 "mismatched sizes, (%ld vs %ld)\n",
190 map->type, map->size, list->map->size);
191 list->map->size = map->size;
192 }
193
194 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
195 *maplist = list;
196 return 0;
197 }
f239b7b0 198 map->handle = vmalloc_user(map->size);
b5e89ed5
DA
199 DRM_DEBUG("%lu %d %p\n",
200 map->size, drm_order(map->size), map->handle);
201 if (!map->handle) {
202 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
1da177e4
LT
203 return -ENOMEM;
204 }
205 map->offset = (unsigned long)map->handle;
b5e89ed5 206 if (map->flags & _DRM_CONTAINS_LOCK) {
1da177e4 207 /* Prevent a 2nd X Server from creating a 2nd lock */
7c1c2871 208 if (dev->primary->master->lock.hw_lock != NULL) {
b5e89ed5
DA
209 vfree(map->handle);
210 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
1da177e4
LT
211 return -EBUSY;
212 }
7c1c2871 213 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
1da177e4
LT
214 }
215 break;
54ba2f76 216 case _DRM_AGP: {
55910517 217 struct drm_agp_mem *entry;
54ba2f76
DA
218 int valid = 0;
219
220 if (!drm_core_has_AGP(dev)) {
221 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
222 return -EINVAL;
223 }
1da177e4 224#ifdef __alpha__
54ba2f76 225 map->offset += dev->hose->mem_space->start;
1da177e4 226#endif
47a184a8
EA
227 /* In some cases (i810 driver), user space may have already
228 * added the AGP base itself, because dev->agp->base previously
229 * only got set during AGP enable. So, only add the base
230 * address if the map's offset isn't already within the
231 * aperture.
54ba2f76 232 */
47a184a8
EA
233 if (map->offset < dev->agp->base ||
234 map->offset > dev->agp->base +
235 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
236 map->offset += dev->agp->base;
237 }
54ba2f76
DA
238 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
239
240 /* This assumes the DRM is in total control of AGP space.
241 * It's not always the case as AGP can be in the control
242 * of user space (i.e. i810 driver). So this loop will get
243 * skipped and we double check that dev->agp->memory is
244 * actually set as well as being invalid before EPERM'ing
245 */
bd1b331f 246 list_for_each_entry(entry, &dev->agp->memory, head) {
54ba2f76
DA
247 if ((map->offset >= entry->bound) &&
248 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
249 valid = 1;
250 break;
251 }
1da177e4 252 }
bd1b331f 253 if (!list_empty(&dev->agp->memory) && !valid) {
54ba2f76
DA
254 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
255 return -EPERM;
256 }
257 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);
258
a2c0a97b
JB
259 break;
260 case _DRM_GEM:
261 DRM_ERROR("tried to rmmap GEM object\n");
1da177e4 262 break;
54ba2f76 263 }
1da177e4
LT
264 case _DRM_SCATTER_GATHER:
265 if (!dev->sg) {
266 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
267 return -EINVAL;
268 }
d1f2b55a 269 map->offset += (unsigned long)dev->sg->virtual;
1da177e4 270 break;
b5e89ed5 271 case _DRM_CONSISTENT:
2d0f9eaf 272 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
9c8da5eb 273 * As we're limiting the address to 2^32-1 (or less),
2d0f9eaf
DA
274 * casting it down to 32 bits is no problem, but we
275 * need to point to a 64bit variable first. */
9c8da5eb
DA
276 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
277 if (!dmah) {
2d0f9eaf
DA
278 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
279 return -ENOMEM;
280 }
9c8da5eb
DA
281 map->handle = dmah->vaddr;
282 map->offset = (unsigned long)dmah->busaddr;
283 kfree(dmah);
2d0f9eaf 284 break;
1da177e4 285 default:
b5e89ed5 286 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
1da177e4
LT
287 return -EINVAL;
288 }
289
290 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
b5e89ed5 291 if (!list) {
85abb3f9 292 if (map->type == _DRM_REGISTERS)
004a7727 293 iounmap(map->handle);
1da177e4
LT
294 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
295 return -EINVAL;
296 }
297 memset(list, 0, sizeof(*list));
298 list->map = map;
299
30e2fb18 300 mutex_lock(&dev->struct_mutex);
bd1b331f 301 list_add(&list->head, &dev->maplist);
8d153f71 302
d1f2b55a 303 /* Assign a 32-bit handle */
30e2fb18 304 /* We do it here so that dev->struct_mutex protects the increment */
8d153f71
TH
305 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
306 map->offset;
a1d0fcf5 307 ret = drm_map_handle(dev, &list->hash, user_token, 0);
8d153f71 308 if (ret) {
85abb3f9 309 if (map->type == _DRM_REGISTERS)
004a7727 310 iounmap(map->handle);
8d153f71
TH
311 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
312 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
313 mutex_unlock(&dev->struct_mutex);
314 return ret;
315 }
316
1545085a 317 list->user_token = list->hash.key << PAGE_SHIFT;
30e2fb18 318 mutex_unlock(&dev->struct_mutex);
1da177e4 319
7c1c2871 320 list->master = dev->primary->master;
89625eb1 321 *maplist = list;
7ab98401 322 return 0;
54ba2f76 323 }
89625eb1 324
84b1fd10 325int drm_addmap(struct drm_device * dev, unsigned int offset,
c60ce623 326 unsigned int size, enum drm_map_type type,
f77d390c 327 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
89625eb1 328{
55910517 329 struct drm_map_list *list;
89625eb1
DA
330 int rc;
331
332 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
333 if (!rc)
334 *map_ptr = list->map;
335 return rc;
336}
b5e89ed5 337
7ab98401
DA
338EXPORT_SYMBOL(drm_addmap);
339
f77d390c
BH
340/**
341 * Ioctl to specify a range of memory that is available for mapping by a
342 * non-root process.
343 *
344 * \param inode device inode.
345 * \param file_priv DRM file private.
346 * \param cmd command.
347 * \param arg pointer to a drm_map structure.
348 * \return zero on success or a negative value on error.
349 *
350 */
c153f45f
EA
351int drm_addmap_ioctl(struct drm_device *dev, void *data,
352 struct drm_file *file_priv)
7ab98401 353{
c153f45f 354 struct drm_map *map = data;
55910517 355 struct drm_map_list *maplist;
7ab98401
DA
356 int err;
357
7c1c2871 358 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
d985c108
DA
359 return -EPERM;
360
c153f45f
EA
361 err = drm_addmap_core(dev, map->offset, map->size, map->type,
362 map->flags, &maplist);
7ab98401 363
b5e89ed5 364 if (err)
7ab98401 365 return err;
d1f2b55a 366
67e1a014 367 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
c153f45f 368 map->handle = (void *)(unsigned long)maplist->user_token;
1da177e4 369 return 0;
88f399cd 370}
1da177e4 371
1da177e4
LT
372/**
373 * Remove a map private from list and deallocate resources if the mapping
374 * isn't in use.
375 *
1da177e4
LT
376 * Searches the map on drm_device::maplist, removes it from the list, see if
377 * its being used, and free any associate resource (such as MTRR's) if it's not
378 * being on use.
379 *
7ab98401 380 * \sa drm_addmap
1da177e4 381 */
f77d390c 382int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
1da177e4 383{
55910517 384 struct drm_map_list *r_list = NULL, *list_t;
836cf046 385 drm_dma_handle_t dmah;
bd1b331f 386 int found = 0;
7c1c2871 387 struct drm_master *master;
1da177e4 388
836cf046 389 /* Find the list entry for the map and remove it */
bd1b331f 390 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
836cf046 391 if (r_list->map == map) {
7c1c2871 392 master = r_list->master;
bd1b331f 393 list_del(&r_list->head);
1545085a
TH
394 drm_ht_remove_key(&dev->map_hash,
395 r_list->user_token >> PAGE_SHIFT);
bd1b331f
DA
396 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
397 found = 1;
836cf046
DA
398 break;
399 }
1da177e4
LT
400 }
401
bd1b331f 402 if (!found)
1da177e4 403 return -EINVAL;
1da177e4 404
836cf046
DA
405 switch (map->type) {
406 case _DRM_REGISTERS:
004a7727 407 iounmap(map->handle);
836cf046
DA
408 /* FALLTHROUGH */
409 case _DRM_FRAME_BUFFER:
410 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
411 int retcode;
b5e89ed5
DA
412 retcode = mtrr_del(map->mtrr, map->offset, map->size);
413 DRM_DEBUG("mtrr_del=%d\n", retcode);
1da177e4 414 }
836cf046
DA
415 break;
416 case _DRM_SHM:
417 vfree(map->handle);
7c1c2871
DA
418 if (master) {
419 if (dev->sigdata.lock == master->lock.hw_lock)
420 dev->sigdata.lock = NULL;
421 master->lock.hw_lock = NULL; /* SHM removed */
422 master->lock.file_priv = NULL;
171901d1 423 wake_up_interruptible_all(&master->lock.lock_queue);
7c1c2871 424 }
836cf046
DA
425 break;
426 case _DRM_AGP:
427 case _DRM_SCATTER_GATHER:
428 break;
429 case _DRM_CONSISTENT:
430 dmah.vaddr = map->handle;
431 dmah.busaddr = map->offset;
432 dmah.size = map->size;
433 __drm_pci_free(dev, &dmah);
434 break;
a2c0a97b
JB
435 case _DRM_GEM:
436 DRM_ERROR("tried to rmmap GEM object\n");
437 break;
1da177e4 438 }
836cf046
DA
439 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
440
1da177e4
LT
441 return 0;
442}
4e74f36d 443EXPORT_SYMBOL(drm_rmmap_locked);
836cf046 444
f77d390c 445int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
836cf046
DA
446{
447 int ret;
448
30e2fb18 449 mutex_lock(&dev->struct_mutex);
836cf046 450 ret = drm_rmmap_locked(dev, map);
30e2fb18 451 mutex_unlock(&dev->struct_mutex);
836cf046
DA
452
453 return ret;
454}
ba8bbcf6 455EXPORT_SYMBOL(drm_rmmap);
7ab98401 456
836cf046
DA
457/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
458 * the last close of the device, and this is necessary for cleanup when things
459 * exit uncleanly. Therefore, having userland manually remove mappings seems
460 * like a pointless exercise since they're going away anyway.
461 *
462 * One use case might be after addmap is allowed for normal users for SHM and
463 * gets used by drivers that the server doesn't need to care about. This seems
464 * unlikely.
f77d390c
BH
465 *
466 * \param inode device inode.
467 * \param file_priv DRM file private.
468 * \param cmd command.
469 * \param arg pointer to a struct drm_map structure.
470 * \return zero on success or a negative value on error.
836cf046 471 */
c153f45f
EA
472int drm_rmmap_ioctl(struct drm_device *dev, void *data,
473 struct drm_file *file_priv)
7ab98401 474{
c153f45f 475 struct drm_map *request = data;
f77d390c 476 struct drm_local_map *map = NULL;
55910517 477 struct drm_map_list *r_list;
836cf046 478 int ret;
7ab98401 479
30e2fb18 480 mutex_lock(&dev->struct_mutex);
bd1b331f 481 list_for_each_entry(r_list, &dev->maplist, head) {
836cf046 482 if (r_list->map &&
c153f45f 483 r_list->user_token == (unsigned long)request->handle &&
836cf046
DA
484 r_list->map->flags & _DRM_REMOVABLE) {
485 map = r_list->map;
486 break;
487 }
488 }
489
490 /* List has wrapped around to the head pointer, or its empty we didn't
491 * find anything.
492 */
bd1b331f 493 if (list_empty(&dev->maplist) || !map) {
30e2fb18 494 mutex_unlock(&dev->struct_mutex);
836cf046
DA
495 return -EINVAL;
496 }
497
836cf046
DA
498 /* Register and framebuffer maps are permanent */
499 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
30e2fb18 500 mutex_unlock(&dev->struct_mutex);
836cf046
DA
501 return 0;
502 }
503
504 ret = drm_rmmap_locked(dev, map);
505
30e2fb18 506 mutex_unlock(&dev->struct_mutex);
836cf046
DA
507
508 return ret;
7ab98401 509}
1da177e4
LT
510
511/**
512 * Cleanup after an error on one of the addbufs() functions.
513 *
836cf046 514 * \param dev DRM device.
1da177e4
LT
515 * \param entry buffer entry where the error occurred.
516 *
517 * Frees any pages and buffers associated with the given entry.
518 */
cdd55a29
DA
519static void drm_cleanup_buf_error(struct drm_device * dev,
520 struct drm_buf_entry * entry)
1da177e4
LT
521{
522 int i;
523
524 if (entry->seg_count) {
525 for (i = 0; i < entry->seg_count; i++) {
526 if (entry->seglist[i]) {
ddf19b97 527 drm_pci_free(dev, entry->seglist[i]);
1da177e4
LT
528 }
529 }
530 drm_free(entry->seglist,
b5e89ed5
DA
531 entry->seg_count *
532 sizeof(*entry->seglist), DRM_MEM_SEGS);
1da177e4
LT
533
534 entry->seg_count = 0;
535 }
536
b5e89ed5
DA
537 if (entry->buf_count) {
538 for (i = 0; i < entry->buf_count; i++) {
1da177e4
LT
539 if (entry->buflist[i].dev_private) {
540 drm_free(entry->buflist[i].dev_private,
b5e89ed5
DA
541 entry->buflist[i].dev_priv_size,
542 DRM_MEM_BUFS);
1da177e4
LT
543 }
544 }
545 drm_free(entry->buflist,
b5e89ed5
DA
546 entry->buf_count *
547 sizeof(*entry->buflist), DRM_MEM_BUFS);
1da177e4
LT
548
549 entry->buf_count = 0;
550 }
551}
552
553#if __OS_HAS_AGP
554/**
d59431bf 555 * Add AGP buffers for DMA transfers.
1da177e4 556 *
84b1fd10 557 * \param dev struct drm_device to which the buffers are to be added.
c60ce623 558 * \param request pointer to a struct drm_buf_desc describing the request.
1da177e4 559 * \return zero on success or a negative number on failure.
b5e89ed5 560 *
1da177e4
LT
561 * After some sanity checks creates a drm_buf structure for each buffer and
562 * reallocates the buffer list of the same size order to accommodate the new
563 * buffers.
564 */
84b1fd10 565int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
1da177e4 566{
cdd55a29
DA
567 struct drm_device_dma *dma = dev->dma;
568 struct drm_buf_entry *entry;
55910517 569 struct drm_agp_mem *agp_entry;
056219e2 570 struct drm_buf *buf;
1da177e4
LT
571 unsigned long offset;
572 unsigned long agp_offset;
573 int count;
574 int order;
575 int size;
576 int alignment;
577 int page_order;
578 int total;
579 int byte_count;
54ba2f76 580 int i, valid;
056219e2 581 struct drm_buf **temp_buflist;
1da177e4 582
b5e89ed5
DA
583 if (!dma)
584 return -EINVAL;
1da177e4 585
d59431bf
DA
586 count = request->count;
587 order = drm_order(request->size);
1da177e4
LT
588 size = 1 << order;
589
b5e89ed5
DA
590 alignment = (request->flags & _DRM_PAGE_ALIGN)
591 ? PAGE_ALIGN(size) : size;
1da177e4
LT
592 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
593 total = PAGE_SIZE << page_order;
594
595 byte_count = 0;
d59431bf 596 agp_offset = dev->agp->base + request->agp_start;
1da177e4 597
b5e89ed5
DA
598 DRM_DEBUG("count: %d\n", count);
599 DRM_DEBUG("order: %d\n", order);
600 DRM_DEBUG("size: %d\n", size);
d985c108 601 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
b5e89ed5
DA
602 DRM_DEBUG("alignment: %d\n", alignment);
603 DRM_DEBUG("page_order: %d\n", page_order);
604 DRM_DEBUG("total: %d\n", total);
1da177e4 605
b5e89ed5
DA
606 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
607 return -EINVAL;
608 if (dev->queue_count)
609 return -EBUSY; /* Not while in use */
1da177e4 610
54ba2f76
DA
611 /* Make sure buffers are located in AGP memory that we own */
612 valid = 0;
bd1b331f 613 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
54ba2f76
DA
614 if ((agp_offset >= agp_entry->bound) &&
615 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
616 valid = 1;
617 break;
618 }
619 }
bd1b331f 620 if (!list_empty(&dev->agp->memory) && !valid) {
54ba2f76
DA
621 DRM_DEBUG("zone invalid\n");
622 return -EINVAL;
623 }
b5e89ed5
DA
624 spin_lock(&dev->count_lock);
625 if (dev->buf_use) {
626 spin_unlock(&dev->count_lock);
1da177e4
LT
627 return -EBUSY;
628 }
b5e89ed5
DA
629 atomic_inc(&dev->buf_alloc);
630 spin_unlock(&dev->count_lock);
1da177e4 631
30e2fb18 632 mutex_lock(&dev->struct_mutex);
1da177e4 633 entry = &dma->bufs[order];
b5e89ed5 634 if (entry->buf_count) {
30e2fb18 635 mutex_unlock(&dev->struct_mutex);
b5e89ed5
DA
636 atomic_dec(&dev->buf_alloc);
637 return -ENOMEM; /* May only call once for each order */
1da177e4
LT
638 }
639
640 if (count < 0 || count > 4096) {
30e2fb18 641 mutex_unlock(&dev->struct_mutex);
b5e89ed5 642 atomic_dec(&dev->buf_alloc);
1da177e4
LT
643 return -EINVAL;
644 }
645
b5e89ed5
DA
646 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
647 DRM_MEM_BUFS);
648 if (!entry->buflist) {
30e2fb18 649 mutex_unlock(&dev->struct_mutex);
b5e89ed5 650 atomic_dec(&dev->buf_alloc);
1da177e4
LT
651 return -ENOMEM;
652 }
b5e89ed5 653 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1da177e4
LT
654
655 entry->buf_size = size;
656 entry->page_order = page_order;
657
658 offset = 0;
659
b5e89ed5
DA
660 while (entry->buf_count < count) {
661 buf = &entry->buflist[entry->buf_count];
662 buf->idx = dma->buf_count + entry->buf_count;
663 buf->total = alignment;
664 buf->order = order;
665 buf->used = 0;
1da177e4 666
b5e89ed5 667 buf->offset = (dma->byte_count + offset);
1da177e4
LT
668 buf->bus_address = agp_offset + offset;
669 buf->address = (void *)(agp_offset + offset);
b5e89ed5 670 buf->next = NULL;
1da177e4
LT
671 buf->waiting = 0;
672 buf->pending = 0;
b5e89ed5 673 init_waitqueue_head(&buf->dma_wait);
6c340eac 674 buf->file_priv = NULL;
1da177e4
LT
675
676 buf->dev_priv_size = dev->driver->dev_priv_size;
b5e89ed5
DA
677 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
678 if (!buf->dev_private) {
1da177e4
LT
679 /* Set count correctly so we free the proper amount. */
680 entry->buf_count = count;
b5e89ed5 681 drm_cleanup_buf_error(dev, entry);
30e2fb18 682 mutex_unlock(&dev->struct_mutex);
b5e89ed5 683 atomic_dec(&dev->buf_alloc);
1da177e4
LT
684 return -ENOMEM;
685 }
b5e89ed5 686 memset(buf->dev_private, 0, buf->dev_priv_size);
1da177e4 687
b5e89ed5 688 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1da177e4
LT
689
690 offset += alignment;
691 entry->buf_count++;
692 byte_count += PAGE_SIZE << page_order;
693 }
694
b5e89ed5 695 DRM_DEBUG("byte_count: %d\n", byte_count);
1da177e4 696
b5e89ed5
DA
697 temp_buflist = drm_realloc(dma->buflist,
698 dma->buf_count * sizeof(*dma->buflist),
699 (dma->buf_count + entry->buf_count)
700 * sizeof(*dma->buflist), DRM_MEM_BUFS);
701 if (!temp_buflist) {
1da177e4 702 /* Free the entry because it isn't valid */
b5e89ed5 703 drm_cleanup_buf_error(dev, entry);
30e2fb18 704 mutex_unlock(&dev->struct_mutex);
b5e89ed5 705 atomic_dec(&dev->buf_alloc);
1da177e4
LT
706 return -ENOMEM;
707 }
708 dma->buflist = temp_buflist;
709
b5e89ed5 710 for (i = 0; i < entry->buf_count; i++) {
1da177e4
LT
711 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
712 }
713
714 dma->buf_count += entry->buf_count;
d985c108
DA
715 dma->seg_count += entry->seg_count;
716 dma->page_count += byte_count >> PAGE_SHIFT;
1da177e4
LT
717 dma->byte_count += byte_count;
718
b5e89ed5
DA
719 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
720 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1da177e4 721
30e2fb18 722 mutex_unlock(&dev->struct_mutex);
1da177e4 723
d59431bf
DA
724 request->count = entry->buf_count;
725 request->size = size;
1da177e4
LT
726
727 dma->flags = _DRM_DMA_USE_AGP;
728
b5e89ed5 729 atomic_dec(&dev->buf_alloc);
1da177e4
LT
730 return 0;
731}
d84f76d3 732EXPORT_SYMBOL(drm_addbufs_agp);
b5e89ed5 733#endif /* __OS_HAS_AGP */
1da177e4 734
84b1fd10 735int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
1da177e4 736{
cdd55a29 737 struct drm_device_dma *dma = dev->dma;
1da177e4
LT
738 int count;
739 int order;
740 int size;
741 int total;
742 int page_order;
cdd55a29 743 struct drm_buf_entry *entry;
ddf19b97 744 drm_dma_handle_t *dmah;
056219e2 745 struct drm_buf *buf;
1da177e4
LT
746 int alignment;
747 unsigned long offset;
748 int i;
749 int byte_count;
750 int page_count;
751 unsigned long *temp_pagelist;
056219e2 752 struct drm_buf **temp_buflist;
1da177e4 753
b5e89ed5
DA
754 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
755 return -EINVAL;
d985c108 756
b5e89ed5
DA
757 if (!dma)
758 return -EINVAL;
1da177e4 759
d985c108
DA
760 if (!capable(CAP_SYS_ADMIN))
761 return -EPERM;
762
d59431bf
DA
763 count = request->count;
764 order = drm_order(request->size);
1da177e4
LT
765 size = 1 << order;
766
b5e89ed5
DA
767 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
768 request->count, request->size, size, order, dev->queue_count);
1da177e4 769
b5e89ed5
DA
770 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
771 return -EINVAL;
772 if (dev->queue_count)
773 return -EBUSY; /* Not while in use */
1da177e4 774
d59431bf 775 alignment = (request->flags & _DRM_PAGE_ALIGN)
b5e89ed5 776 ? PAGE_ALIGN(size) : size;
1da177e4
LT
777 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
778 total = PAGE_SIZE << page_order;
779
b5e89ed5
DA
780 spin_lock(&dev->count_lock);
781 if (dev->buf_use) {
782 spin_unlock(&dev->count_lock);
1da177e4
LT
783 return -EBUSY;
784 }
b5e89ed5
DA
785 atomic_inc(&dev->buf_alloc);
786 spin_unlock(&dev->count_lock);
1da177e4 787
30e2fb18 788 mutex_lock(&dev->struct_mutex);
1da177e4 789 entry = &dma->bufs[order];
b5e89ed5 790 if (entry->buf_count) {
30e2fb18 791 mutex_unlock(&dev->struct_mutex);
b5e89ed5 792 atomic_dec(&dev->buf_alloc);
1da177e4
LT
793 return -ENOMEM; /* May only call once for each order */
794 }
795
796 if (count < 0 || count > 4096) {
30e2fb18 797 mutex_unlock(&dev->struct_mutex);
b5e89ed5 798 atomic_dec(&dev->buf_alloc);
1da177e4
LT
799 return -EINVAL;
800 }
801
b5e89ed5
DA
802 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
803 DRM_MEM_BUFS);
804 if (!entry->buflist) {
30e2fb18 805 mutex_unlock(&dev->struct_mutex);
b5e89ed5 806 atomic_dec(&dev->buf_alloc);
1da177e4
LT
807 return -ENOMEM;
808 }
b5e89ed5
DA
809 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
810
811 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
812 DRM_MEM_SEGS);
813 if (!entry->seglist) {
814 drm_free(entry->buflist,
815 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
30e2fb18 816 mutex_unlock(&dev->struct_mutex);
b5e89ed5 817 atomic_dec(&dev->buf_alloc);
1da177e4
LT
818 return -ENOMEM;
819 }
b5e89ed5 820 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
1da177e4
LT
821
822 /* Keep the original pagelist until we know all the allocations
823 * have succeeded
824 */
b5e89ed5
DA
825 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
826 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
1da177e4 827 if (!temp_pagelist) {
b5e89ed5
DA
828 drm_free(entry->buflist,
829 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
830 drm_free(entry->seglist,
831 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
30e2fb18 832 mutex_unlock(&dev->struct_mutex);
b5e89ed5 833 atomic_dec(&dev->buf_alloc);
1da177e4
LT
834 return -ENOMEM;
835 }
836 memcpy(temp_pagelist,
b5e89ed5
DA
837 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
838 DRM_DEBUG("pagelist: %d entries\n",
839 dma->page_count + (count << page_order));
1da177e4 840
b5e89ed5 841 entry->buf_size = size;
1da177e4
LT
842 entry->page_order = page_order;
843 byte_count = 0;
844 page_count = 0;
845
b5e89ed5 846 while (entry->buf_count < count) {
bc5f4523 847
ddf19b97 848 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
bc5f4523 849
ddf19b97 850 if (!dmah) {
1da177e4
LT
851 /* Set count correctly so we free the proper amount. */
852 entry->buf_count = count;
853 entry->seg_count = count;
854 drm_cleanup_buf_error(dev, entry);
b5e89ed5
DA
855 drm_free(temp_pagelist,
856 (dma->page_count + (count << page_order))
857 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
30e2fb18 858 mutex_unlock(&dev->struct_mutex);
b5e89ed5 859 atomic_dec(&dev->buf_alloc);
1da177e4
LT
860 return -ENOMEM;
861 }
ddf19b97 862 entry->seglist[entry->seg_count++] = dmah;
b5e89ed5
DA
863 for (i = 0; i < (1 << page_order); i++) {
864 DRM_DEBUG("page %d @ 0x%08lx\n",
865 dma->page_count + page_count,
ddf19b97 866 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
1da177e4 867 temp_pagelist[dma->page_count + page_count++]
ddf19b97 868 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
1da177e4 869 }
b5e89ed5
DA
870 for (offset = 0;
871 offset + size <= total && entry->buf_count < count;
872 offset += alignment, ++entry->buf_count) {
873 buf = &entry->buflist[entry->buf_count];
874 buf->idx = dma->buf_count + entry->buf_count;
875 buf->total = alignment;
876 buf->order = order;
877 buf->used = 0;
878 buf->offset = (dma->byte_count + byte_count + offset);
ddf19b97
DA
879 buf->address = (void *)(dmah->vaddr + offset);
880 buf->bus_address = dmah->busaddr + offset;
b5e89ed5 881 buf->next = NULL;
1da177e4
LT
882 buf->waiting = 0;
883 buf->pending = 0;
b5e89ed5 884 init_waitqueue_head(&buf->dma_wait);
6c340eac 885 buf->file_priv = NULL;
1da177e4
LT
886
887 buf->dev_priv_size = dev->driver->dev_priv_size;
b5e89ed5
DA
888 buf->dev_private = drm_alloc(buf->dev_priv_size,
889 DRM_MEM_BUFS);
890 if (!buf->dev_private) {
1da177e4
LT
891 /* Set count correctly so we free the proper amount. */
892 entry->buf_count = count;
893 entry->seg_count = count;
b5e89ed5
DA
894 drm_cleanup_buf_error(dev, entry);
895 drm_free(temp_pagelist,
896 (dma->page_count +
897 (count << page_order))
898 * sizeof(*dma->pagelist),
899 DRM_MEM_PAGES);
30e2fb18 900 mutex_unlock(&dev->struct_mutex);
b5e89ed5 901 atomic_dec(&dev->buf_alloc);
1da177e4
LT
902 return -ENOMEM;
903 }
b5e89ed5 904 memset(buf->dev_private, 0, buf->dev_priv_size);
1da177e4 905
b5e89ed5
DA
906 DRM_DEBUG("buffer %d @ %p\n",
907 entry->buf_count, buf->address);
1da177e4
LT
908 }
909 byte_count += PAGE_SIZE << page_order;
910 }
911
b5e89ed5
DA
912 temp_buflist = drm_realloc(dma->buflist,
913 dma->buf_count * sizeof(*dma->buflist),
914 (dma->buf_count + entry->buf_count)
915 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1da177e4
LT
916 if (!temp_buflist) {
917 /* Free the entry because it isn't valid */
b5e89ed5
DA
918 drm_cleanup_buf_error(dev, entry);
919 drm_free(temp_pagelist,
920 (dma->page_count + (count << page_order))
921 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
30e2fb18 922 mutex_unlock(&dev->struct_mutex);
b5e89ed5 923 atomic_dec(&dev->buf_alloc);
1da177e4
LT
924 return -ENOMEM;
925 }
926 dma->buflist = temp_buflist;
927
b5e89ed5 928 for (i = 0; i < entry->buf_count; i++) {
1da177e4
LT
929 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
930 }
931
932 /* No allocations failed, so now we can replace the orginal pagelist
933 * with the new one.
934 */
935 if (dma->page_count) {
936 drm_free(dma->pagelist,
b5e89ed5
DA
937 dma->page_count * sizeof(*dma->pagelist),
938 DRM_MEM_PAGES);
1da177e4
LT
939 }
940 dma->pagelist = temp_pagelist;
941
942 dma->buf_count += entry->buf_count;
943 dma->seg_count += entry->seg_count;
944 dma->page_count += entry->seg_count << page_order;
945 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
946
30e2fb18 947 mutex_unlock(&dev->struct_mutex);
1da177e4 948
d59431bf
DA
949 request->count = entry->buf_count;
950 request->size = size;
1da177e4 951
3417f33e
GS
952 if (request->flags & _DRM_PCI_BUFFER_RO)
953 dma->flags = _DRM_DMA_USE_PCI_RO;
954
b5e89ed5 955 atomic_dec(&dev->buf_alloc);
1da177e4
LT
956 return 0;
957
958}
d84f76d3 959EXPORT_SYMBOL(drm_addbufs_pci);
1da177e4 960
84b1fd10 961static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
1da177e4 962{
cdd55a29
DA
963 struct drm_device_dma *dma = dev->dma;
964 struct drm_buf_entry *entry;
056219e2 965 struct drm_buf *buf;
1da177e4
LT
966 unsigned long offset;
967 unsigned long agp_offset;
968 int count;
969 int order;
970 int size;
971 int alignment;
972 int page_order;
973 int total;
974 int byte_count;
975 int i;
056219e2 976 struct drm_buf **temp_buflist;
1da177e4 977
b5e89ed5
DA
978 if (!drm_core_check_feature(dev, DRIVER_SG))
979 return -EINVAL;
980
981 if (!dma)
982 return -EINVAL;
1da177e4 983
d985c108
DA
984 if (!capable(CAP_SYS_ADMIN))
985 return -EPERM;
986
d59431bf
DA
987 count = request->count;
988 order = drm_order(request->size);
1da177e4
LT
989 size = 1 << order;
990
b5e89ed5
DA
991 alignment = (request->flags & _DRM_PAGE_ALIGN)
992 ? PAGE_ALIGN(size) : size;
1da177e4
LT
993 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
994 total = PAGE_SIZE << page_order;
995
996 byte_count = 0;
d59431bf 997 agp_offset = request->agp_start;
1da177e4 998
b5e89ed5
DA
999 DRM_DEBUG("count: %d\n", count);
1000 DRM_DEBUG("order: %d\n", order);
1001 DRM_DEBUG("size: %d\n", size);
1002 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1003 DRM_DEBUG("alignment: %d\n", alignment);
1004 DRM_DEBUG("page_order: %d\n", page_order);
1005 DRM_DEBUG("total: %d\n", total);
1da177e4 1006
b5e89ed5
DA
1007 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1008 return -EINVAL;
1009 if (dev->queue_count)
1010 return -EBUSY; /* Not while in use */
1da177e4 1011
b5e89ed5
DA
1012 spin_lock(&dev->count_lock);
1013 if (dev->buf_use) {
1014 spin_unlock(&dev->count_lock);
1da177e4
LT
1015 return -EBUSY;
1016 }
b5e89ed5
DA
1017 atomic_inc(&dev->buf_alloc);
1018 spin_unlock(&dev->count_lock);
1da177e4 1019
30e2fb18 1020 mutex_lock(&dev->struct_mutex);
1da177e4 1021 entry = &dma->bufs[order];
b5e89ed5 1022 if (entry->buf_count) {
30e2fb18 1023 mutex_unlock(&dev->struct_mutex);
b5e89ed5
DA
1024 atomic_dec(&dev->buf_alloc);
1025 return -ENOMEM; /* May only call once for each order */
1da177e4
LT
1026 }
1027
1028 if (count < 0 || count > 4096) {
30e2fb18 1029 mutex_unlock(&dev->struct_mutex);
b5e89ed5 1030 atomic_dec(&dev->buf_alloc);
1da177e4
LT
1031 return -EINVAL;
1032 }
1033
b5e89ed5
DA
1034 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1035 DRM_MEM_BUFS);
1036 if (!entry->buflist) {
30e2fb18 1037 mutex_unlock(&dev->struct_mutex);
b5e89ed5 1038 atomic_dec(&dev->buf_alloc);
1da177e4
LT
1039 return -ENOMEM;
1040 }
b5e89ed5 1041 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1da177e4
LT
1042
1043 entry->buf_size = size;
1044 entry->page_order = page_order;
1045
1046 offset = 0;
1047
b5e89ed5
DA
1048 while (entry->buf_count < count) {
1049 buf = &entry->buflist[entry->buf_count];
1050 buf->idx = dma->buf_count + entry->buf_count;
1051 buf->total = alignment;
1052 buf->order = order;
1053 buf->used = 0;
1da177e4 1054
b5e89ed5 1055 buf->offset = (dma->byte_count + offset);
1da177e4 1056 buf->bus_address = agp_offset + offset;
b5e89ed5 1057 buf->address = (void *)(agp_offset + offset
d1f2b55a 1058 + (unsigned long)dev->sg->virtual);
b5e89ed5 1059 buf->next = NULL;
1da177e4
LT
1060 buf->waiting = 0;
1061 buf->pending = 0;
b5e89ed5 1062 init_waitqueue_head(&buf->dma_wait);
6c340eac 1063 buf->file_priv = NULL;
1da177e4
LT
1064
1065 buf->dev_priv_size = dev->driver->dev_priv_size;
b5e89ed5
DA
1066 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1067 if (!buf->dev_private) {
1da177e4
LT
1068 /* Set count correctly so we free the proper amount. */
1069 entry->buf_count = count;
b5e89ed5 1070 drm_cleanup_buf_error(dev, entry);
30e2fb18 1071 mutex_unlock(&dev->struct_mutex);
b5e89ed5 1072 atomic_dec(&dev->buf_alloc);
1da177e4
LT
1073 return -ENOMEM;
1074 }
1075
b5e89ed5 1076 memset(buf->dev_private, 0, buf->dev_priv_size);
1da177e4 1077
b5e89ed5 1078 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1da177e4
LT
1079
1080 offset += alignment;
1081 entry->buf_count++;
1082 byte_count += PAGE_SIZE << page_order;
1083 }
1084
b5e89ed5 1085 DRM_DEBUG("byte_count: %d\n", byte_count);
1da177e4 1086
b5e89ed5
DA
1087 temp_buflist = drm_realloc(dma->buflist,
1088 dma->buf_count * sizeof(*dma->buflist),
1089 (dma->buf_count + entry->buf_count)
1090 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1091 if (!temp_buflist) {
1da177e4 1092 /* Free the entry because it isn't valid */
b5e89ed5 1093 drm_cleanup_buf_error(dev, entry);
30e2fb18 1094 mutex_unlock(&dev->struct_mutex);
b5e89ed5 1095 atomic_dec(&dev->buf_alloc);
1da177e4
LT
1096 return -ENOMEM;
1097 }
1098 dma->buflist = temp_buflist;
1099
b5e89ed5 1100 for (i = 0; i < entry->buf_count; i++) {
1da177e4
LT
1101 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1102 }
1103
1104 dma->buf_count += entry->buf_count;
d985c108
DA
1105 dma->seg_count += entry->seg_count;
1106 dma->page_count += byte_count >> PAGE_SHIFT;
1da177e4
LT
1107 dma->byte_count += byte_count;
1108
b5e89ed5
DA
1109 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1110 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1da177e4 1111
30e2fb18 1112 mutex_unlock(&dev->struct_mutex);
1da177e4 1113
d59431bf
DA
1114 request->count = entry->buf_count;
1115 request->size = size;
1da177e4
LT
1116
1117 dma->flags = _DRM_DMA_USE_SG;
1118
b5e89ed5 1119 atomic_dec(&dev->buf_alloc);
1da177e4
LT
1120 return 0;
1121}
1122
84b1fd10 1123static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
b84397d6 1124{
cdd55a29
DA
1125 struct drm_device_dma *dma = dev->dma;
1126 struct drm_buf_entry *entry;
056219e2 1127 struct drm_buf *buf;
b84397d6
DA
1128 unsigned long offset;
1129 unsigned long agp_offset;
1130 int count;
1131 int order;
1132 int size;
1133 int alignment;
1134 int page_order;
1135 int total;
1136 int byte_count;
1137 int i;
056219e2 1138 struct drm_buf **temp_buflist;
b84397d6
DA
1139
1140 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1141 return -EINVAL;
b5e89ed5 1142
b84397d6
DA
1143 if (!dma)
1144 return -EINVAL;
1145
d985c108
DA
1146 if (!capable(CAP_SYS_ADMIN))
1147 return -EPERM;
1148
d59431bf
DA
1149 count = request->count;
1150 order = drm_order(request->size);
b84397d6
DA
1151 size = 1 << order;
1152
d59431bf 1153 alignment = (request->flags & _DRM_PAGE_ALIGN)
b84397d6
DA
1154 ? PAGE_ALIGN(size) : size;
1155 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1156 total = PAGE_SIZE << page_order;
1157
1158 byte_count = 0;
d59431bf 1159 agp_offset = request->agp_start;
b84397d6
DA
1160
1161 DRM_DEBUG("count: %d\n", count);
1162 DRM_DEBUG("order: %d\n", order);
1163 DRM_DEBUG("size: %d\n", size);
1164 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1165 DRM_DEBUG("alignment: %d\n", alignment);
1166 DRM_DEBUG("page_order: %d\n", page_order);
1167 DRM_DEBUG("total: %d\n", total);
1168
1169 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1170 return -EINVAL;
1171 if (dev->queue_count)
1172 return -EBUSY; /* Not while in use */
1173
1174 spin_lock(&dev->count_lock);
1175 if (dev->buf_use) {
1176 spin_unlock(&dev->count_lock);
1177 return -EBUSY;
1178 }
1179 atomic_inc(&dev->buf_alloc);
1180 spin_unlock(&dev->count_lock);
1181
30e2fb18 1182 mutex_lock(&dev->struct_mutex);
b84397d6
DA
1183 entry = &dma->bufs[order];
1184 if (entry->buf_count) {
30e2fb18 1185 mutex_unlock(&dev->struct_mutex);
b84397d6
DA
1186 atomic_dec(&dev->buf_alloc);
1187 return -ENOMEM; /* May only call once for each order */
1188 }
1189
1190 if (count < 0 || count > 4096) {
30e2fb18 1191 mutex_unlock(&dev->struct_mutex);
b84397d6
DA
1192 atomic_dec(&dev->buf_alloc);
1193 return -EINVAL;
1194 }
1195
1196 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1197 DRM_MEM_BUFS);
1198 if (!entry->buflist) {
30e2fb18 1199 mutex_unlock(&dev->struct_mutex);
b84397d6
DA
1200 atomic_dec(&dev->buf_alloc);
1201 return -ENOMEM;
1202 }
1203 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1204
1205 entry->buf_size = size;
1206 entry->page_order = page_order;
1207
1208 offset = 0;
1209
1210 while (entry->buf_count < count) {
1211 buf = &entry->buflist[entry->buf_count];
1212 buf->idx = dma->buf_count + entry->buf_count;
1213 buf->total = alignment;
1214 buf->order = order;
1215 buf->used = 0;
1216
1217 buf->offset = (dma->byte_count + offset);
1218 buf->bus_address = agp_offset + offset;
1219 buf->address = (void *)(agp_offset + offset);
1220 buf->next = NULL;
1221 buf->waiting = 0;
1222 buf->pending = 0;
1223 init_waitqueue_head(&buf->dma_wait);
6c340eac 1224 buf->file_priv = NULL;
b84397d6
DA
1225
1226 buf->dev_priv_size = dev->driver->dev_priv_size;
1227 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1228 if (!buf->dev_private) {
1229 /* Set count correctly so we free the proper amount. */
1230 entry->buf_count = count;
1231 drm_cleanup_buf_error(dev, entry);
30e2fb18 1232 mutex_unlock(&dev->struct_mutex);
b84397d6
DA
1233 atomic_dec(&dev->buf_alloc);
1234 return -ENOMEM;
1235 }
1236 memset(buf->dev_private, 0, buf->dev_priv_size);
1237
1238 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1239
1240 offset += alignment;
1241 entry->buf_count++;
1242 byte_count += PAGE_SIZE << page_order;
1243 }
1244
1245 DRM_DEBUG("byte_count: %d\n", byte_count);
1246
1247 temp_buflist = drm_realloc(dma->buflist,
1248 dma->buf_count * sizeof(*dma->buflist),
1249 (dma->buf_count + entry->buf_count)
1250 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1251 if (!temp_buflist) {
1252 /* Free the entry because it isn't valid */
1253 drm_cleanup_buf_error(dev, entry);
30e2fb18 1254 mutex_unlock(&dev->struct_mutex);
b84397d6
DA
1255 atomic_dec(&dev->buf_alloc);
1256 return -ENOMEM;
1257 }
1258 dma->buflist = temp_buflist;
1259
1260 for (i = 0; i < entry->buf_count; i++) {
1261 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1262 }
1263
1264 dma->buf_count += entry->buf_count;
d985c108
DA
1265 dma->seg_count += entry->seg_count;
1266 dma->page_count += byte_count >> PAGE_SHIFT;
b84397d6
DA
1267 dma->byte_count += byte_count;
1268
1269 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1270 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1271
30e2fb18 1272 mutex_unlock(&dev->struct_mutex);
b84397d6 1273
d59431bf
DA
1274 request->count = entry->buf_count;
1275 request->size = size;
b84397d6
DA
1276
1277 dma->flags = _DRM_DMA_USE_FB;
1278
1279 atomic_dec(&dev->buf_alloc);
1280 return 0;
1281}
d985c108 1282
b84397d6 1283
1da177e4
LT
1284/**
1285 * Add buffers for DMA transfers (ioctl).
1286 *
1287 * \param inode device inode.
6c340eac 1288 * \param file_priv DRM file private.
1da177e4 1289 * \param cmd command.
c60ce623 1290 * \param arg pointer to a struct drm_buf_desc request.
1da177e4
LT
1291 * \return zero on success or a negative number on failure.
1292 *
1293 * According with the memory type specified in drm_buf_desc::flags and the
1294 * build options, it dispatches the call either to addbufs_agp(),
1295 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1296 * PCI memory respectively.
1297 */
c153f45f
EA
1298int drm_addbufs(struct drm_device *dev, void *data,
1299 struct drm_file *file_priv)
1da177e4 1300{
c153f45f 1301 struct drm_buf_desc *request = data;
d59431bf 1302 int ret;
b5e89ed5 1303
1da177e4
LT
1304 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1305 return -EINVAL;
1306
1da177e4 1307#if __OS_HAS_AGP
c153f45f
EA
1308 if (request->flags & _DRM_AGP_BUFFER)
1309 ret = drm_addbufs_agp(dev, request);
1da177e4
LT
1310 else
1311#endif
c153f45f
EA
1312 if (request->flags & _DRM_SG_BUFFER)
1313 ret = drm_addbufs_sg(dev, request);
1314 else if (request->flags & _DRM_FB_BUFFER)
1315 ret = drm_addbufs_fb(dev, request);
1da177e4 1316 else
c153f45f 1317 ret = drm_addbufs_pci(dev, request);
d59431bf 1318
d59431bf 1319 return ret;
1da177e4
LT
1320}
1321
1da177e4
LT
1322/**
1323 * Get information about the buffer mappings.
1324 *
1325 * This was originally mean for debugging purposes, or by a sophisticated
1326 * client library to determine how best to use the available buffers (e.g.,
1327 * large buffers can be used for image transfer).
1328 *
1329 * \param inode device inode.
6c340eac 1330 * \param file_priv DRM file private.
1da177e4
LT
1331 * \param cmd command.
1332 * \param arg pointer to a drm_buf_info structure.
1333 * \return zero on success or a negative number on failure.
1334 *
1335 * Increments drm_device::buf_use while holding the drm_device::count_lock
1336 * lock, preventing of allocating more buffers after this call. Information
1337 * about each requested buffer is then copied into user space.
1338 */
c153f45f
EA
1339int drm_infobufs(struct drm_device *dev, void *data,
1340 struct drm_file *file_priv)
1da177e4 1341{
cdd55a29 1342 struct drm_device_dma *dma = dev->dma;
c153f45f 1343 struct drm_buf_info *request = data;
1da177e4
LT
1344 int i;
1345 int count;
1346
1347 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1348 return -EINVAL;
1349
b5e89ed5
DA
1350 if (!dma)
1351 return -EINVAL;
1da177e4 1352
b5e89ed5
DA
1353 spin_lock(&dev->count_lock);
1354 if (atomic_read(&dev->buf_alloc)) {
1355 spin_unlock(&dev->count_lock);
1da177e4
LT
1356 return -EBUSY;
1357 }
1358 ++dev->buf_use; /* Can't allocate more after this call */
b5e89ed5 1359 spin_unlock(&dev->count_lock);
1da177e4 1360
b5e89ed5
DA
1361 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1362 if (dma->bufs[i].buf_count)
1363 ++count;
1da177e4
LT
1364 }
1365
b5e89ed5 1366 DRM_DEBUG("count = %d\n", count);
1da177e4 1367
c153f45f 1368 if (request->count >= count) {
b5e89ed5
DA
1369 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1370 if (dma->bufs[i].buf_count) {
c60ce623 1371 struct drm_buf_desc __user *to =
c153f45f 1372 &request->list[count];
cdd55a29
DA
1373 struct drm_buf_entry *from = &dma->bufs[i];
1374 struct drm_freelist *list = &dma->bufs[i].freelist;
b5e89ed5
DA
1375 if (copy_to_user(&to->count,
1376 &from->buf_count,
1377 sizeof(from->buf_count)) ||
1378 copy_to_user(&to->size,
1379 &from->buf_size,
1380 sizeof(from->buf_size)) ||
1381 copy_to_user(&to->low_mark,
1382 &list->low_mark,
1383 sizeof(list->low_mark)) ||
1384 copy_to_user(&to->high_mark,
1385 &list->high_mark,
1386 sizeof(list->high_mark)))
1da177e4
LT
1387 return -EFAULT;
1388
b5e89ed5
DA
1389 DRM_DEBUG("%d %d %d %d %d\n",
1390 i,
1391 dma->bufs[i].buf_count,
1392 dma->bufs[i].buf_size,
1393 dma->bufs[i].freelist.low_mark,
1394 dma->bufs[i].freelist.high_mark);
1da177e4
LT
1395 ++count;
1396 }
1397 }
1398 }
c153f45f 1399 request->count = count;
1da177e4
LT
1400
1401 return 0;
1402}
1403
1404/**
1405 * Specifies a low and high water mark for buffer allocation
1406 *
1407 * \param inode device inode.
6c340eac 1408 * \param file_priv DRM file private.
1da177e4
LT
1409 * \param cmd command.
1410 * \param arg a pointer to a drm_buf_desc structure.
1411 * \return zero on success or a negative number on failure.
1412 *
1413 * Verifies that the size order is bounded between the admissible orders and
1414 * updates the respective drm_device_dma::bufs entry low and high water mark.
1415 *
1416 * \note This ioctl is deprecated and mostly never used.
1417 */
c153f45f
EA
1418int drm_markbufs(struct drm_device *dev, void *data,
1419 struct drm_file *file_priv)
1da177e4 1420{
cdd55a29 1421 struct drm_device_dma *dma = dev->dma;
c153f45f 1422 struct drm_buf_desc *request = data;
1da177e4 1423 int order;
cdd55a29 1424 struct drm_buf_entry *entry;
1da177e4
LT
1425
1426 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1427 return -EINVAL;
1428
b5e89ed5
DA
1429 if (!dma)
1430 return -EINVAL;
1da177e4 1431
b5e89ed5 1432 DRM_DEBUG("%d, %d, %d\n",
c153f45f
EA
1433 request->size, request->low_mark, request->high_mark);
1434 order = drm_order(request->size);
b5e89ed5
DA
1435 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1436 return -EINVAL;
1da177e4
LT
1437 entry = &dma->bufs[order];
1438
c153f45f 1439 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
1da177e4 1440 return -EINVAL;
c153f45f 1441 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
1da177e4
LT
1442 return -EINVAL;
1443
c153f45f
EA
1444 entry->freelist.low_mark = request->low_mark;
1445 entry->freelist.high_mark = request->high_mark;
1da177e4
LT
1446
1447 return 0;
1448}
1449
1450/**
b5e89ed5 1451 * Unreserve the buffers in list, previously reserved using drmDMA.
1da177e4
LT
1452 *
1453 * \param inode device inode.
6c340eac 1454 * \param file_priv DRM file private.
1da177e4
LT
1455 * \param cmd command.
1456 * \param arg pointer to a drm_buf_free structure.
1457 * \return zero on success or a negative number on failure.
b5e89ed5 1458 *
1da177e4
LT
1459 * Calls free_buffer() for each used buffer.
1460 * This function is primarily used for debugging.
1461 */
c153f45f
EA
1462int drm_freebufs(struct drm_device *dev, void *data,
1463 struct drm_file *file_priv)
1da177e4 1464{
cdd55a29 1465 struct drm_device_dma *dma = dev->dma;
c153f45f 1466 struct drm_buf_free *request = data;
1da177e4
LT
1467 int i;
1468 int idx;
056219e2 1469 struct drm_buf *buf;
1da177e4
LT
1470
1471 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1472 return -EINVAL;
1473
b5e89ed5
DA
1474 if (!dma)
1475 return -EINVAL;
1da177e4 1476
c153f45f
EA
1477 DRM_DEBUG("%d\n", request->count);
1478 for (i = 0; i < request->count; i++) {
1479 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1da177e4 1480 return -EFAULT;
b5e89ed5
DA
1481 if (idx < 0 || idx >= dma->buf_count) {
1482 DRM_ERROR("Index %d (of %d max)\n",
1483 idx, dma->buf_count - 1);
1da177e4
LT
1484 return -EINVAL;
1485 }
1486 buf = dma->buflist[idx];
6c340eac 1487 if (buf->file_priv != file_priv) {
b5e89ed5 1488 DRM_ERROR("Process %d freeing buffer not owned\n",
ba25f9dc 1489 task_pid_nr(current));
1da177e4
LT
1490 return -EINVAL;
1491 }
b5e89ed5 1492 drm_free_buffer(dev, buf);
1da177e4
LT
1493 }
1494
1495 return 0;
1496}
1497
1498/**
1499 * Maps all of the DMA buffers into client-virtual space (ioctl).
1500 *
1501 * \param inode device inode.
6c340eac 1502 * \param file_priv DRM file private.
1da177e4
LT
1503 * \param cmd command.
1504 * \param arg pointer to a drm_buf_map structure.
1505 * \return zero on success or a negative number on failure.
1506 *
3417f33e
GS
1507 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1508 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1509 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1510 * drm_mmap_dma().
1da177e4 1511 */
c153f45f
EA
1512int drm_mapbufs(struct drm_device *dev, void *data,
1513 struct drm_file *file_priv)
1da177e4 1514{
cdd55a29 1515 struct drm_device_dma *dma = dev->dma;
1da177e4
LT
1516 int retcode = 0;
1517 const int zero = 0;
1518 unsigned long virtual;
1519 unsigned long address;
c153f45f 1520 struct drm_buf_map *request = data;
1da177e4
LT
1521 int i;
1522
1523 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1524 return -EINVAL;
1525
b5e89ed5
DA
1526 if (!dma)
1527 return -EINVAL;
1da177e4 1528
b5e89ed5
DA
1529 spin_lock(&dev->count_lock);
1530 if (atomic_read(&dev->buf_alloc)) {
1531 spin_unlock(&dev->count_lock);
1da177e4
LT
1532 return -EBUSY;
1533 }
1534 dev->buf_use++; /* Can't allocate more after this call */
b5e89ed5 1535 spin_unlock(&dev->count_lock);
1da177e4 1536
c153f45f 1537 if (request->count >= dma->buf_count) {
b84397d6 1538 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
b5e89ed5 1539 || (drm_core_check_feature(dev, DRIVER_SG)
b84397d6
DA
1540 && (dma->flags & _DRM_DMA_USE_SG))
1541 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1542 && (dma->flags & _DRM_DMA_USE_FB))) {
f77d390c 1543 struct drm_local_map *map = dev->agp_buffer_map;
d1f2b55a 1544 unsigned long token = dev->agp_buffer_token;
1da177e4 1545
b5e89ed5 1546 if (!map) {
1da177e4
LT
1547 retcode = -EINVAL;
1548 goto done;
1549 }
b5e89ed5 1550 down_write(&current->mm->mmap_sem);
6c340eac 1551 virtual = do_mmap(file_priv->filp, 0, map->size,
b5e89ed5 1552 PROT_READ | PROT_WRITE,
c153f45f
EA
1553 MAP_SHARED,
1554 token);
b5e89ed5 1555 up_write(&current->mm->mmap_sem);
1da177e4 1556 } else {
b5e89ed5 1557 down_write(&current->mm->mmap_sem);
6c340eac 1558 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
b5e89ed5
DA
1559 PROT_READ | PROT_WRITE,
1560 MAP_SHARED, 0);
1561 up_write(&current->mm->mmap_sem);
1da177e4 1562 }
b5e89ed5 1563 if (virtual > -1024UL) {
1da177e4
LT
1564 /* Real error */
1565 retcode = (signed long)virtual;
1566 goto done;
1567 }
c153f45f 1568 request->virtual = (void __user *)virtual;
1da177e4 1569
b5e89ed5 1570 for (i = 0; i < dma->buf_count; i++) {
c153f45f 1571 if (copy_to_user(&request->list[i].idx,
b5e89ed5 1572 &dma->buflist[i]->idx,
c153f45f 1573 sizeof(request->list[0].idx))) {
1da177e4
LT
1574 retcode = -EFAULT;
1575 goto done;
1576 }
c153f45f 1577 if (copy_to_user(&request->list[i].total,
b5e89ed5 1578 &dma->buflist[i]->total,
c153f45f 1579 sizeof(request->list[0].total))) {
1da177e4
LT
1580 retcode = -EFAULT;
1581 goto done;
1582 }
c153f45f 1583 if (copy_to_user(&request->list[i].used,
b5e89ed5 1584 &zero, sizeof(zero))) {
1da177e4
LT
1585 retcode = -EFAULT;
1586 goto done;
1587 }
b5e89ed5 1588 address = virtual + dma->buflist[i]->offset; /* *** */
c153f45f 1589 if (copy_to_user(&request->list[i].address,
b5e89ed5 1590 &address, sizeof(address))) {
1da177e4
LT
1591 retcode = -EFAULT;
1592 goto done;
1593 }
1594 }
1595 }
b5e89ed5 1596 done:
c153f45f
EA
1597 request->count = dma->buf_count;
1598 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1da177e4
LT
1599
1600 return retcode;
1601}
1602
836cf046
DA
1603/**
1604 * Compute size order. Returns the exponent of the smaller power of two which
1605 * is greater or equal to given number.
b5e89ed5 1606 *
836cf046
DA
1607 * \param size size.
1608 * \return order.
1609 *
1610 * \todo Can be made faster.
1611 */
b5e89ed5 1612int drm_order(unsigned long size)
836cf046
DA
1613{
1614 int order;
1615 unsigned long tmp;
1616
b5e89ed5 1617 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
836cf046
DA
1618
1619 if (size & (size - 1))
1620 ++order;
1621
1622 return order;
1623}
1624EXPORT_SYMBOL(drm_order);
This page took 0.417296 seconds and 5 git commands to generate.