drm_bufs.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. /**
  2. * \file drm_bufs.c
  3. * Generic buffer template
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
  10. *
  11. * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include <linux/vmalloc.h>
  35. #include <linux/slab.h>
  36. #include <linux/log2.h>
  37. #include <linux/export.h>
  38. #include <asm/shmparam.h>
  39. #include <drm/drmP.h>
  40. static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
  41. struct drm_local_map *map)
  42. {
  43. struct drm_map_list *entry;
  44. list_for_each_entry(entry, &dev->maplist, head) {
  45. /*
  46. * Because the kernel-userspace ABI is fixed at a 32-bit offset
  47. * while PCI resources may live above that, we only compare the
  48. * lower 32 bits of the map offset for maps of type
  49. * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
  50. * It is assumed that if a driver have more than one resource
  51. * of each type, the lower 32 bits are different.
  52. */
  53. if (!entry->map ||
  54. map->type != entry->map->type ||
  55. entry->master != dev->primary->master)
  56. continue;
  57. switch (map->type) {
  58. case _DRM_SHM:
  59. if (map->flags != _DRM_CONTAINS_LOCK)
  60. break;
  61. return entry;
  62. case _DRM_REGISTERS:
  63. case _DRM_FRAME_BUFFER:
  64. if ((entry->map->offset & 0xffffffff) ==
  65. (map->offset & 0xffffffff))
  66. return entry;
  67. default: /* Make gcc happy */
  68. ;
  69. }
  70. if (entry->map->offset == map->offset)
  71. return entry;
  72. }
  73. return NULL;
  74. }
  75. static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
  76. unsigned long user_token, int hashed_handle, int shm)
  77. {
  78. int use_hashed_handle, shift;
  79. unsigned long add;
  80. #if (BITS_PER_LONG == 64)
  81. use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
  82. #elif (BITS_PER_LONG == 32)
  83. use_hashed_handle = hashed_handle;
  84. #else
  85. #error Unsupported long size. Neither 64 nor 32 bits.
  86. #endif
  87. if (!use_hashed_handle) {
  88. int ret;
  89. hash->key = user_token >> PAGE_SHIFT;
  90. ret = drm_ht_insert_item(&dev->map_hash, hash);
  91. if (ret != -EINVAL)
  92. return ret;
  93. }
  94. shift = 0;
  95. add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
  96. if (shm && (SHMLBA > PAGE_SIZE)) {
  97. int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
  98. /* For shared memory, we have to preserve the SHMLBA
  99. * bits of the eventual vma->vm_pgoff value during
  100. * mmap(). Otherwise we run into cache aliasing problems
  101. * on some platforms. On these platforms, the pgoff of
  102. * a mmap() request is used to pick a suitable virtual
  103. * address for the mmap() region such that it will not
  104. * cause cache aliasing problems.
  105. *
  106. * Therefore, make sure the SHMLBA relevant bits of the
  107. * hash value we use are equal to those in the original
  108. * kernel virtual address.
  109. */
  110. shift = bits;
  111. add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
  112. }
  113. return drm_ht_just_insert_please(&dev->map_hash, hash,
  114. user_token, 32 - PAGE_SHIFT - 3,
  115. shift, add);
  116. }
  117. /**
  118. * Core function to create a range of memory available for mapping by a
  119. * non-root process.
  120. *
  121. * Adjusts the memory offset to its absolute value according to the mapping
  122. * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
  123. * applicable and if supported by the kernel.
  124. */
  125. static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
  126. unsigned int size, enum drm_map_type type,
  127. enum drm_map_flags flags,
  128. struct drm_map_list ** maplist)
  129. {
  130. struct drm_local_map *map;
  131. struct drm_map_list *list;
  132. drm_dma_handle_t *dmah;
  133. unsigned long user_token;
  134. int ret;
  135. map = kmalloc(sizeof(*map), GFP_KERNEL);
  136. if (!map)
  137. return -ENOMEM;
  138. map->offset = offset;
  139. map->size = size;
  140. map->flags = flags;
  141. map->type = type;
  142. /* Only allow shared memory to be removable since we only keep enough
  143. * book keeping information about shared memory to allow for removal
  144. * when processes fork.
  145. */
  146. if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
  147. kfree(map);
  148. return -EINVAL;
  149. }
  150. DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
  151. (unsigned long long)map->offset, map->size, map->type);
  152. /* page-align _DRM_SHM maps. They are allocated here so there is no security
  153. * hole created by that and it works around various broken drivers that use
  154. * a non-aligned quantity to map the SAREA. --BenH
  155. */
  156. if (map->type == _DRM_SHM)
  157. map->size = PAGE_ALIGN(map->size);
  158. if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
  159. kfree(map);
  160. return -EINVAL;
  161. }
  162. map->mtrr = -1;
  163. map->handle = NULL;
  164. switch (map->type) {
  165. case _DRM_REGISTERS:
  166. case _DRM_FRAME_BUFFER:
  167. #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
  168. if (map->offset + (map->size-1) < map->offset ||
  169. map->offset < virt_to_phys(high_memory)) {
  170. kfree(map);
  171. return -EINVAL;
  172. }
  173. #endif
  174. /* Some drivers preinitialize some maps, without the X Server
  175. * needing to be aware of it. Therefore, we just return success
  176. * when the server tries to create a duplicate map.
  177. */
  178. list = drm_find_matching_map(dev, map);
  179. if (list != NULL) {
  180. if (list->map->size != map->size) {
  181. DRM_DEBUG("Matching maps of type %d with "
  182. "mismatched sizes, (%ld vs %ld)\n",
  183. map->type, map->size,
  184. list->map->size);
  185. list->map->size = map->size;
  186. }
  187. kfree(map);
  188. *maplist = list;
  189. return 0;
  190. }
  191. if (map->type == _DRM_FRAME_BUFFER ||
  192. (map->flags & _DRM_WRITE_COMBINING)) {
  193. map->mtrr =
  194. arch_phys_wc_add(map->offset, map->size);
  195. }
  196. if (map->type == _DRM_REGISTERS) {
  197. if (map->flags & _DRM_WRITE_COMBINING)
  198. map->handle = ioremap_wc(map->offset,
  199. map->size);
  200. else
  201. map->handle = ioremap(map->offset, map->size);
  202. if (!map->handle) {
  203. kfree(map);
  204. return -ENOMEM;
  205. }
  206. }
  207. break;
  208. case _DRM_SHM:
  209. list = drm_find_matching_map(dev, map);
  210. if (list != NULL) {
  211. if(list->map->size != map->size) {
  212. DRM_DEBUG("Matching maps of type %d with "
  213. "mismatched sizes, (%ld vs %ld)\n",
  214. map->type, map->size, list->map->size);
  215. list->map->size = map->size;
  216. }
  217. kfree(map);
  218. *maplist = list;
  219. return 0;
  220. }
  221. map->handle = vmalloc_user(map->size);
  222. DRM_DEBUG("%lu %d %p\n",
  223. map->size, order_base_2(map->size), map->handle);
  224. if (!map->handle) {
  225. kfree(map);
  226. return -ENOMEM;
  227. }
  228. map->offset = (unsigned long)map->handle;
  229. if (map->flags & _DRM_CONTAINS_LOCK) {
  230. /* Prevent a 2nd X Server from creating a 2nd lock */
  231. if (dev->primary->master->lock.hw_lock != NULL) {
  232. vfree(map->handle);
  233. kfree(map);
  234. return -EBUSY;
  235. }
  236. dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
  237. }
  238. break;
  239. case _DRM_AGP: {
  240. struct drm_agp_mem *entry;
  241. int valid = 0;
  242. if (!dev->agp) {
  243. kfree(map);
  244. return -EINVAL;
  245. }
  246. #ifdef __alpha__
  247. map->offset += dev->hose->mem_space->start;
  248. #endif
  249. /* In some cases (i810 driver), user space may have already
  250. * added the AGP base itself, because dev->agp->base previously
  251. * only got set during AGP enable. So, only add the base
  252. * address if the map's offset isn't already within the
  253. * aperture.
  254. */
  255. if (map->offset < dev->agp->base ||
  256. map->offset > dev->agp->base +
  257. dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
  258. map->offset += dev->agp->base;
  259. }
  260. map->mtrr = dev->agp->agp_mtrr; /* for getmap */
  261. /* This assumes the DRM is in total control of AGP space.
  262. * It's not always the case as AGP can be in the control
  263. * of user space (i.e. i810 driver). So this loop will get
  264. * skipped and we double check that dev->agp->memory is
  265. * actually set as well as being invalid before EPERM'ing
  266. */
  267. list_for_each_entry(entry, &dev->agp->memory, head) {
  268. if ((map->offset >= entry->bound) &&
  269. (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
  270. valid = 1;
  271. break;
  272. }
  273. }
  274. if (!list_empty(&dev->agp->memory) && !valid) {
  275. kfree(map);
  276. return -EPERM;
  277. }
  278. DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
  279. (unsigned long long)map->offset, map->size);
  280. break;
  281. }
  282. case _DRM_SCATTER_GATHER:
  283. if (!dev->sg) {
  284. kfree(map);
  285. return -EINVAL;
  286. }
  287. map->offset += (unsigned long)dev->sg->virtual;
  288. break;
  289. case _DRM_CONSISTENT:
  290. /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
  291. * As we're limiting the address to 2^32-1 (or less),
  292. * casting it down to 32 bits is no problem, but we
  293. * need to point to a 64bit variable first. */
  294. dmah = drm_pci_alloc(dev, map->size, map->size);
  295. if (!dmah) {
  296. kfree(map);
  297. return -ENOMEM;
  298. }
  299. map->handle = dmah->vaddr;
  300. map->offset = (unsigned long)dmah->busaddr;
  301. kfree(dmah);
  302. break;
  303. default:
  304. kfree(map);
  305. return -EINVAL;
  306. }
  307. list = kzalloc(sizeof(*list), GFP_KERNEL);
  308. if (!list) {
  309. if (map->type == _DRM_REGISTERS)
  310. iounmap(map->handle);
  311. kfree(map);
  312. return -EINVAL;
  313. }
  314. list->map = map;
  315. mutex_lock(&dev->struct_mutex);
  316. list_add(&list->head, &dev->maplist);
  317. /* Assign a 32-bit handle */
  318. /* We do it here so that dev->struct_mutex protects the increment */
  319. user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
  320. map->offset;
  321. ret = drm_map_handle(dev, &list->hash, user_token, 0,
  322. (map->type == _DRM_SHM));
  323. if (ret) {
  324. if (map->type == _DRM_REGISTERS)
  325. iounmap(map->handle);
  326. kfree(map);
  327. kfree(list);
  328. mutex_unlock(&dev->struct_mutex);
  329. return ret;
  330. }
  331. list->user_token = list->hash.key << PAGE_SHIFT;
  332. mutex_unlock(&dev->struct_mutex);
  333. if (!(map->flags & _DRM_DRIVER))
  334. list->master = dev->primary->master;
  335. *maplist = list;
  336. return 0;
  337. }
  338. int drm_addmap(struct drm_device * dev, resource_size_t offset,
  339. unsigned int size, enum drm_map_type type,
  340. enum drm_map_flags flags, struct drm_local_map ** map_ptr)
  341. {
  342. struct drm_map_list *list;
  343. int rc;
  344. rc = drm_addmap_core(dev, offset, size, type, flags, &list);
  345. if (!rc)
  346. *map_ptr = list->map;
  347. return rc;
  348. }
  349. EXPORT_SYMBOL(drm_addmap);
  350. /**
  351. * Ioctl to specify a range of memory that is available for mapping by a
  352. * non-root process.
  353. *
  354. * \param inode device inode.
  355. * \param file_priv DRM file private.
  356. * \param cmd command.
  357. * \param arg pointer to a drm_map structure.
  358. * \return zero on success or a negative value on error.
  359. *
  360. */
  361. int drm_addmap_ioctl(struct drm_device *dev, void *data,
  362. struct drm_file *file_priv)
  363. {
  364. struct drm_map *map = data;
  365. struct drm_map_list *maplist;
  366. int err;
  367. if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
  368. return -EPERM;
  369. err = drm_addmap_core(dev, map->offset, map->size, map->type,
  370. map->flags, &maplist);
  371. if (err)
  372. return err;
  373. /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
  374. map->handle = (void *)(unsigned long)maplist->user_token;
  375. /*
  376. * It appears that there are no users of this value whatsoever --
  377. * drmAddMap just discards it. Let's not encourage its use.
  378. * (Keeping drm_addmap_core's returned mtrr value would be wrong --
  379. * it's not a real mtrr index anymore.)
  380. */
  381. map->mtrr = -1;
  382. return 0;
  383. }
  384. /**
  385. * Remove a map private from list and deallocate resources if the mapping
  386. * isn't in use.
  387. *
  388. * Searches the map on drm_device::maplist, removes it from the list, see if
  389. * its being used, and free any associate resource (such as MTRR's) if it's not
  390. * being on use.
  391. *
  392. * \sa drm_addmap
  393. */
  394. int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
  395. {
  396. struct drm_map_list *r_list = NULL, *list_t;
  397. drm_dma_handle_t dmah;
  398. int found = 0;
  399. struct drm_master *master;
  400. /* Find the list entry for the map and remove it */
  401. list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
  402. if (r_list->map == map) {
  403. master = r_list->master;
  404. list_del(&r_list->head);
  405. drm_ht_remove_key(&dev->map_hash,
  406. r_list->user_token >> PAGE_SHIFT);
  407. kfree(r_list);
  408. found = 1;
  409. break;
  410. }
  411. }
  412. if (!found)
  413. return -EINVAL;
  414. switch (map->type) {
  415. case _DRM_REGISTERS:
  416. iounmap(map->handle);
  417. /* FALLTHROUGH */
  418. case _DRM_FRAME_BUFFER:
  419. arch_phys_wc_del(map->mtrr);
  420. break;
  421. case _DRM_SHM:
  422. vfree(map->handle);
  423. if (master) {
  424. if (dev->sigdata.lock == master->lock.hw_lock)
  425. dev->sigdata.lock = NULL;
  426. master->lock.hw_lock = NULL; /* SHM removed */
  427. master->lock.file_priv = NULL;
  428. wake_up_interruptible_all(&master->lock.lock_queue);
  429. }
  430. break;
  431. case _DRM_AGP:
  432. case _DRM_SCATTER_GATHER:
  433. break;
  434. case _DRM_CONSISTENT:
  435. dmah.vaddr = map->handle;
  436. dmah.busaddr = map->offset;
  437. dmah.size = map->size;
  438. __drm_pci_free(dev, &dmah);
  439. break;
  440. }
  441. kfree(map);
  442. return 0;
  443. }
  444. EXPORT_SYMBOL(drm_rmmap_locked);
  445. int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
  446. {
  447. int ret;
  448. mutex_lock(&dev->struct_mutex);
  449. ret = drm_rmmap_locked(dev, map);
  450. mutex_unlock(&dev->struct_mutex);
  451. return ret;
  452. }
  453. EXPORT_SYMBOL(drm_rmmap);
  454. /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
  455. * the last close of the device, and this is necessary for cleanup when things
  456. * exit uncleanly. Therefore, having userland manually remove mappings seems
  457. * like a pointless exercise since they're going away anyway.
  458. *
  459. * One use case might be after addmap is allowed for normal users for SHM and
  460. * gets used by drivers that the server doesn't need to care about. This seems
  461. * unlikely.
  462. *
  463. * \param inode device inode.
  464. * \param file_priv DRM file private.
  465. * \param cmd command.
  466. * \param arg pointer to a struct drm_map structure.
  467. * \return zero on success or a negative value on error.
  468. */
  469. int drm_rmmap_ioctl(struct drm_device *dev, void *data,
  470. struct drm_file *file_priv)
  471. {
  472. struct drm_map *request = data;
  473. struct drm_local_map *map = NULL;
  474. struct drm_map_list *r_list;
  475. int ret;
  476. mutex_lock(&dev->struct_mutex);
  477. list_for_each_entry(r_list, &dev->maplist, head) {
  478. if (r_list->map &&
  479. r_list->user_token == (unsigned long)request->handle &&
  480. r_list->map->flags & _DRM_REMOVABLE) {
  481. map = r_list->map;
  482. break;
  483. }
  484. }
  485. /* List has wrapped around to the head pointer, or its empty we didn't
  486. * find anything.
  487. */
  488. if (list_empty(&dev->maplist) || !map) {
  489. mutex_unlock(&dev->struct_mutex);
  490. return -EINVAL;
  491. }
  492. /* Register and framebuffer maps are permanent */
  493. if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
  494. mutex_unlock(&dev->struct_mutex);
  495. return 0;
  496. }
  497. ret = drm_rmmap_locked(dev, map);
  498. mutex_unlock(&dev->struct_mutex);
  499. return ret;
  500. }
  501. /**
  502. * Cleanup after an error on one of the addbufs() functions.
  503. *
  504. * \param dev DRM device.
  505. * \param entry buffer entry where the error occurred.
  506. *
  507. * Frees any pages and buffers associated with the given entry.
  508. */
  509. static void drm_cleanup_buf_error(struct drm_device * dev,
  510. struct drm_buf_entry * entry)
  511. {
  512. int i;
  513. if (entry->seg_count) {
  514. for (i = 0; i < entry->seg_count; i++) {
  515. if (entry->seglist[i]) {
  516. drm_pci_free(dev, entry->seglist[i]);
  517. }
  518. }
  519. kfree(entry->seglist);
  520. entry->seg_count = 0;
  521. }
  522. if (entry->buf_count) {
  523. for (i = 0; i < entry->buf_count; i++) {
  524. kfree(entry->buflist[i].dev_private);
  525. }
  526. kfree(entry->buflist);
  527. entry->buf_count = 0;
  528. }
  529. }
  530. #if __OS_HAS_AGP
  531. /**
  532. * Add AGP buffers for DMA transfers.
  533. *
  534. * \param dev struct drm_device to which the buffers are to be added.
  535. * \param request pointer to a struct drm_buf_desc describing the request.
  536. * \return zero on success or a negative number on failure.
  537. *
  538. * After some sanity checks creates a drm_buf structure for each buffer and
  539. * reallocates the buffer list of the same size order to accommodate the new
  540. * buffers.
  541. */
  542. int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
  543. {
  544. struct drm_device_dma *dma = dev->dma;
  545. struct drm_buf_entry *entry;
  546. struct drm_agp_mem *agp_entry;
  547. struct drm_buf *buf;
  548. unsigned long offset;
  549. unsigned long agp_offset;
  550. int count;
  551. int order;
  552. int size;
  553. int alignment;
  554. int page_order;
  555. int total;
  556. int byte_count;
  557. int i, valid;
  558. struct drm_buf **temp_buflist;
  559. if (!dma)
  560. return -EINVAL;
  561. count = request->count;
  562. order = order_base_2(request->size);
  563. size = 1 << order;
  564. alignment = (request->flags & _DRM_PAGE_ALIGN)
  565. ? PAGE_ALIGN(size) : size;
  566. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  567. total = PAGE_SIZE << page_order;
  568. byte_count = 0;
  569. agp_offset = dev->agp->base + request->agp_start;
  570. DRM_DEBUG("count: %d\n", count);
  571. DRM_DEBUG("order: %d\n", order);
  572. DRM_DEBUG("size: %d\n", size);
  573. DRM_DEBUG("agp_offset: %lx\n", agp_offset);
  574. DRM_DEBUG("alignment: %d\n", alignment);
  575. DRM_DEBUG("page_order: %d\n", page_order);
  576. DRM_DEBUG("total: %d\n", total);
  577. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  578. return -EINVAL;
  579. /* Make sure buffers are located in AGP memory that we own */
  580. valid = 0;
  581. list_for_each_entry(agp_entry, &dev->agp->memory, head) {
  582. if ((agp_offset >= agp_entry->bound) &&
  583. (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
  584. valid = 1;
  585. break;
  586. }
  587. }
  588. if (!list_empty(&dev->agp->memory) && !valid) {
  589. DRM_DEBUG("zone invalid\n");
  590. return -EINVAL;
  591. }
  592. spin_lock(&dev->buf_lock);
  593. if (dev->buf_use) {
  594. spin_unlock(&dev->buf_lock);
  595. return -EBUSY;
  596. }
  597. atomic_inc(&dev->buf_alloc);
  598. spin_unlock(&dev->buf_lock);
  599. mutex_lock(&dev->struct_mutex);
  600. entry = &dma->bufs[order];
  601. if (entry->buf_count) {
  602. mutex_unlock(&dev->struct_mutex);
  603. atomic_dec(&dev->buf_alloc);
  604. return -ENOMEM; /* May only call once for each order */
  605. }
  606. if (count < 0 || count > 4096) {
  607. mutex_unlock(&dev->struct_mutex);
  608. atomic_dec(&dev->buf_alloc);
  609. return -EINVAL;
  610. }
  611. entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  612. if (!entry->buflist) {
  613. mutex_unlock(&dev->struct_mutex);
  614. atomic_dec(&dev->buf_alloc);
  615. return -ENOMEM;
  616. }
  617. entry->buf_size = size;
  618. entry->page_order = page_order;
  619. offset = 0;
  620. while (entry->buf_count < count) {
  621. buf = &entry->buflist[entry->buf_count];
  622. buf->idx = dma->buf_count + entry->buf_count;
  623. buf->total = alignment;
  624. buf->order = order;
  625. buf->used = 0;
  626. buf->offset = (dma->byte_count + offset);
  627. buf->bus_address = agp_offset + offset;
  628. buf->address = (void *)(agp_offset + offset);
  629. buf->next = NULL;
  630. buf->waiting = 0;
  631. buf->pending = 0;
  632. buf->file_priv = NULL;
  633. buf->dev_priv_size = dev->driver->dev_priv_size;
  634. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  635. if (!buf->dev_private) {
  636. /* Set count correctly so we free the proper amount. */
  637. entry->buf_count = count;
  638. drm_cleanup_buf_error(dev, entry);
  639. mutex_unlock(&dev->struct_mutex);
  640. atomic_dec(&dev->buf_alloc);
  641. return -ENOMEM;
  642. }
  643. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  644. offset += alignment;
  645. entry->buf_count++;
  646. byte_count += PAGE_SIZE << page_order;
  647. }
  648. DRM_DEBUG("byte_count: %d\n", byte_count);
  649. temp_buflist = krealloc(dma->buflist,
  650. (dma->buf_count + entry->buf_count) *
  651. sizeof(*dma->buflist), GFP_KERNEL);
  652. if (!temp_buflist) {
  653. /* Free the entry because it isn't valid */
  654. drm_cleanup_buf_error(dev, entry);
  655. mutex_unlock(&dev->struct_mutex);
  656. atomic_dec(&dev->buf_alloc);
  657. return -ENOMEM;
  658. }
  659. dma->buflist = temp_buflist;
  660. for (i = 0; i < entry->buf_count; i++) {
  661. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  662. }
  663. dma->buf_count += entry->buf_count;
  664. dma->seg_count += entry->seg_count;
  665. dma->page_count += byte_count >> PAGE_SHIFT;
  666. dma->byte_count += byte_count;
  667. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  668. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  669. mutex_unlock(&dev->struct_mutex);
  670. request->count = entry->buf_count;
  671. request->size = size;
  672. dma->flags = _DRM_DMA_USE_AGP;
  673. atomic_dec(&dev->buf_alloc);
  674. return 0;
  675. }
  676. EXPORT_SYMBOL(drm_addbufs_agp);
  677. #endif /* __OS_HAS_AGP */
  678. int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
  679. {
  680. struct drm_device_dma *dma = dev->dma;
  681. int count;
  682. int order;
  683. int size;
  684. int total;
  685. int page_order;
  686. struct drm_buf_entry *entry;
  687. drm_dma_handle_t *dmah;
  688. struct drm_buf *buf;
  689. int alignment;
  690. unsigned long offset;
  691. int i;
  692. int byte_count;
  693. int page_count;
  694. unsigned long *temp_pagelist;
  695. struct drm_buf **temp_buflist;
  696. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
  697. return -EINVAL;
  698. if (!dma)
  699. return -EINVAL;
  700. if (!capable(CAP_SYS_ADMIN))
  701. return -EPERM;
  702. count = request->count;
  703. order = order_base_2(request->size);
  704. size = 1 << order;
  705. DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
  706. request->count, request->size, size, order);
  707. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  708. return -EINVAL;
  709. alignment = (request->flags & _DRM_PAGE_ALIGN)
  710. ? PAGE_ALIGN(size) : size;
  711. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  712. total = PAGE_SIZE << page_order;
  713. spin_lock(&dev->buf_lock);
  714. if (dev->buf_use) {
  715. spin_unlock(&dev->buf_lock);
  716. return -EBUSY;
  717. }
  718. atomic_inc(&dev->buf_alloc);
  719. spin_unlock(&dev->buf_lock);
  720. mutex_lock(&dev->struct_mutex);
  721. entry = &dma->bufs[order];
  722. if (entry->buf_count) {
  723. mutex_unlock(&dev->struct_mutex);
  724. atomic_dec(&dev->buf_alloc);
  725. return -ENOMEM; /* May only call once for each order */
  726. }
  727. if (count < 0 || count > 4096) {
  728. mutex_unlock(&dev->struct_mutex);
  729. atomic_dec(&dev->buf_alloc);
  730. return -EINVAL;
  731. }
  732. entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  733. if (!entry->buflist) {
  734. mutex_unlock(&dev->struct_mutex);
  735. atomic_dec(&dev->buf_alloc);
  736. return -ENOMEM;
  737. }
  738. entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
  739. if (!entry->seglist) {
  740. kfree(entry->buflist);
  741. mutex_unlock(&dev->struct_mutex);
  742. atomic_dec(&dev->buf_alloc);
  743. return -ENOMEM;
  744. }
  745. /* Keep the original pagelist until we know all the allocations
  746. * have succeeded
  747. */
  748. temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
  749. sizeof(*dma->pagelist), GFP_KERNEL);
  750. if (!temp_pagelist) {
  751. kfree(entry->buflist);
  752. kfree(entry->seglist);
  753. mutex_unlock(&dev->struct_mutex);
  754. atomic_dec(&dev->buf_alloc);
  755. return -ENOMEM;
  756. }
  757. memcpy(temp_pagelist,
  758. dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
  759. DRM_DEBUG("pagelist: %d entries\n",
  760. dma->page_count + (count << page_order));
  761. entry->buf_size = size;
  762. entry->page_order = page_order;
  763. byte_count = 0;
  764. page_count = 0;
  765. while (entry->buf_count < count) {
  766. dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
  767. if (!dmah) {
  768. /* Set count correctly so we free the proper amount. */
  769. entry->buf_count = count;
  770. entry->seg_count = count;
  771. drm_cleanup_buf_error(dev, entry);
  772. kfree(temp_pagelist);
  773. mutex_unlock(&dev->struct_mutex);
  774. atomic_dec(&dev->buf_alloc);
  775. return -ENOMEM;
  776. }
  777. entry->seglist[entry->seg_count++] = dmah;
  778. for (i = 0; i < (1 << page_order); i++) {
  779. DRM_DEBUG("page %d @ 0x%08lx\n",
  780. dma->page_count + page_count,
  781. (unsigned long)dmah->vaddr + PAGE_SIZE * i);
  782. temp_pagelist[dma->page_count + page_count++]
  783. = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
  784. }
  785. for (offset = 0;
  786. offset + size <= total && entry->buf_count < count;
  787. offset += alignment, ++entry->buf_count) {
  788. buf = &entry->buflist[entry->buf_count];
  789. buf->idx = dma->buf_count + entry->buf_count;
  790. buf->total = alignment;
  791. buf->order = order;
  792. buf->used = 0;
  793. buf->offset = (dma->byte_count + byte_count + offset);
  794. buf->address = (void *)(dmah->vaddr + offset);
  795. buf->bus_address = dmah->busaddr + offset;
  796. buf->next = NULL;
  797. buf->waiting = 0;
  798. buf->pending = 0;
  799. buf->file_priv = NULL;
  800. buf->dev_priv_size = dev->driver->dev_priv_size;
  801. buf->dev_private = kzalloc(buf->dev_priv_size,
  802. GFP_KERNEL);
  803. if (!buf->dev_private) {
  804. /* Set count correctly so we free the proper amount. */
  805. entry->buf_count = count;
  806. entry->seg_count = count;
  807. drm_cleanup_buf_error(dev, entry);
  808. kfree(temp_pagelist);
  809. mutex_unlock(&dev->struct_mutex);
  810. atomic_dec(&dev->buf_alloc);
  811. return -ENOMEM;
  812. }
  813. DRM_DEBUG("buffer %d @ %p\n",
  814. entry->buf_count, buf->address);
  815. }
  816. byte_count += PAGE_SIZE << page_order;
  817. }
  818. temp_buflist = krealloc(dma->buflist,
  819. (dma->buf_count + entry->buf_count) *
  820. sizeof(*dma->buflist), GFP_KERNEL);
  821. if (!temp_buflist) {
  822. /* Free the entry because it isn't valid */
  823. drm_cleanup_buf_error(dev, entry);
  824. kfree(temp_pagelist);
  825. mutex_unlock(&dev->struct_mutex);
  826. atomic_dec(&dev->buf_alloc);
  827. return -ENOMEM;
  828. }
  829. dma->buflist = temp_buflist;
  830. for (i = 0; i < entry->buf_count; i++) {
  831. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  832. }
  833. /* No allocations failed, so now we can replace the original pagelist
  834. * with the new one.
  835. */
  836. if (dma->page_count) {
  837. kfree(dma->pagelist);
  838. }
  839. dma->pagelist = temp_pagelist;
  840. dma->buf_count += entry->buf_count;
  841. dma->seg_count += entry->seg_count;
  842. dma->page_count += entry->seg_count << page_order;
  843. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  844. mutex_unlock(&dev->struct_mutex);
  845. request->count = entry->buf_count;
  846. request->size = size;
  847. if (request->flags & _DRM_PCI_BUFFER_RO)
  848. dma->flags = _DRM_DMA_USE_PCI_RO;
  849. atomic_dec(&dev->buf_alloc);
  850. return 0;
  851. }
  852. EXPORT_SYMBOL(drm_addbufs_pci);
  853. static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
  854. {
  855. struct drm_device_dma *dma = dev->dma;
  856. struct drm_buf_entry *entry;
  857. struct drm_buf *buf;
  858. unsigned long offset;
  859. unsigned long agp_offset;
  860. int count;
  861. int order;
  862. int size;
  863. int alignment;
  864. int page_order;
  865. int total;
  866. int byte_count;
  867. int i;
  868. struct drm_buf **temp_buflist;
  869. if (!drm_core_check_feature(dev, DRIVER_SG))
  870. return -EINVAL;
  871. if (!dma)
  872. return -EINVAL;
  873. if (!capable(CAP_SYS_ADMIN))
  874. return -EPERM;
  875. count = request->count;
  876. order = order_base_2(request->size);
  877. size = 1 << order;
  878. alignment = (request->flags & _DRM_PAGE_ALIGN)
  879. ? PAGE_ALIGN(size) : size;
  880. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  881. total = PAGE_SIZE << page_order;
  882. byte_count = 0;
  883. agp_offset = request->agp_start;
  884. DRM_DEBUG("count: %d\n", count);
  885. DRM_DEBUG("order: %d\n", order);
  886. DRM_DEBUG("size: %d\n", size);
  887. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  888. DRM_DEBUG("alignment: %d\n", alignment);
  889. DRM_DEBUG("page_order: %d\n", page_order);
  890. DRM_DEBUG("total: %d\n", total);
  891. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  892. return -EINVAL;
  893. spin_lock(&dev->buf_lock);
  894. if (dev->buf_use) {
  895. spin_unlock(&dev->buf_lock);
  896. return -EBUSY;
  897. }
  898. atomic_inc(&dev->buf_alloc);
  899. spin_unlock(&dev->buf_lock);
  900. mutex_lock(&dev->struct_mutex);
  901. entry = &dma->bufs[order];
  902. if (entry->buf_count) {
  903. mutex_unlock(&dev->struct_mutex);
  904. atomic_dec(&dev->buf_alloc);
  905. return -ENOMEM; /* May only call once for each order */
  906. }
  907. if (count < 0 || count > 4096) {
  908. mutex_unlock(&dev->struct_mutex);
  909. atomic_dec(&dev->buf_alloc);
  910. return -EINVAL;
  911. }
  912. entry->buflist = kzalloc(count * sizeof(*entry->buflist),
  913. GFP_KERNEL);
  914. if (!entry->buflist) {
  915. mutex_unlock(&dev->struct_mutex);
  916. atomic_dec(&dev->buf_alloc);
  917. return -ENOMEM;
  918. }
  919. entry->buf_size = size;
  920. entry->page_order = page_order;
  921. offset = 0;
  922. while (entry->buf_count < count) {
  923. buf = &entry->buflist[entry->buf_count];
  924. buf->idx = dma->buf_count + entry->buf_count;
  925. buf->total = alignment;
  926. buf->order = order;
  927. buf->used = 0;
  928. buf->offset = (dma->byte_count + offset);
  929. buf->bus_address = agp_offset + offset;
  930. buf->address = (void *)(agp_offset + offset
  931. + (unsigned long)dev->sg->virtual);
  932. buf->next = NULL;
  933. buf->waiting = 0;
  934. buf->pending = 0;
  935. buf->file_priv = NULL;
  936. buf->dev_priv_size = dev->driver->dev_priv_size;
  937. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  938. if (!buf->dev_private) {
  939. /* Set count correctly so we free the proper amount. */
  940. entry->buf_count = count;
  941. drm_cleanup_buf_error(dev, entry);
  942. mutex_unlock(&dev->struct_mutex);
  943. atomic_dec(&dev->buf_alloc);
  944. return -ENOMEM;
  945. }
  946. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  947. offset += alignment;
  948. entry->buf_count++;
  949. byte_count += PAGE_SIZE << page_order;
  950. }
  951. DRM_DEBUG("byte_count: %d\n", byte_count);
  952. temp_buflist = krealloc(dma->buflist,
  953. (dma->buf_count + entry->buf_count) *
  954. sizeof(*dma->buflist), GFP_KERNEL);
  955. if (!temp_buflist) {
  956. /* Free the entry because it isn't valid */
  957. drm_cleanup_buf_error(dev, entry);
  958. mutex_unlock(&dev->struct_mutex);
  959. atomic_dec(&dev->buf_alloc);
  960. return -ENOMEM;
  961. }
  962. dma->buflist = temp_buflist;
  963. for (i = 0; i < entry->buf_count; i++) {
  964. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  965. }
  966. dma->buf_count += entry->buf_count;
  967. dma->seg_count += entry->seg_count;
  968. dma->page_count += byte_count >> PAGE_SHIFT;
  969. dma->byte_count += byte_count;
  970. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  971. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  972. mutex_unlock(&dev->struct_mutex);
  973. request->count = entry->buf_count;
  974. request->size = size;
  975. dma->flags = _DRM_DMA_USE_SG;
  976. atomic_dec(&dev->buf_alloc);
  977. return 0;
  978. }
  979. /**
  980. * Add buffers for DMA transfers (ioctl).
  981. *
  982. * \param inode device inode.
  983. * \param file_priv DRM file private.
  984. * \param cmd command.
  985. * \param arg pointer to a struct drm_buf_desc request.
  986. * \return zero on success or a negative number on failure.
  987. *
  988. * According with the memory type specified in drm_buf_desc::flags and the
  989. * build options, it dispatches the call either to addbufs_agp(),
  990. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  991. * PCI memory respectively.
  992. */
  993. int drm_addbufs(struct drm_device *dev, void *data,
  994. struct drm_file *file_priv)
  995. {
  996. struct drm_buf_desc *request = data;
  997. int ret;
  998. if (drm_core_check_feature(dev, DRIVER_MODESET))
  999. return -EINVAL;
  1000. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1001. return -EINVAL;
  1002. #if __OS_HAS_AGP
  1003. if (request->flags & _DRM_AGP_BUFFER)
  1004. ret = drm_addbufs_agp(dev, request);
  1005. else
  1006. #endif
  1007. if (request->flags & _DRM_SG_BUFFER)
  1008. ret = drm_addbufs_sg(dev, request);
  1009. else if (request->flags & _DRM_FB_BUFFER)
  1010. ret = -EINVAL;
  1011. else
  1012. ret = drm_addbufs_pci(dev, request);
  1013. return ret;
  1014. }
  1015. /**
  1016. * Get information about the buffer mappings.
  1017. *
  1018. * This was originally mean for debugging purposes, or by a sophisticated
  1019. * client library to determine how best to use the available buffers (e.g.,
  1020. * large buffers can be used for image transfer).
  1021. *
  1022. * \param inode device inode.
  1023. * \param file_priv DRM file private.
  1024. * \param cmd command.
  1025. * \param arg pointer to a drm_buf_info structure.
  1026. * \return zero on success or a negative number on failure.
  1027. *
  1028. * Increments drm_device::buf_use while holding the drm_device::buf_lock
  1029. * lock, preventing of allocating more buffers after this call. Information
  1030. * about each requested buffer is then copied into user space.
  1031. */
  1032. int drm_infobufs(struct drm_device *dev, void *data,
  1033. struct drm_file *file_priv)
  1034. {
  1035. struct drm_device_dma *dma = dev->dma;
  1036. struct drm_buf_info *request = data;
  1037. int i;
  1038. int count;
  1039. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1040. return -EINVAL;
  1041. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1042. return -EINVAL;
  1043. if (!dma)
  1044. return -EINVAL;
  1045. spin_lock(&dev->buf_lock);
  1046. if (atomic_read(&dev->buf_alloc)) {
  1047. spin_unlock(&dev->buf_lock);
  1048. return -EBUSY;
  1049. }
  1050. ++dev->buf_use; /* Can't allocate more after this call */
  1051. spin_unlock(&dev->buf_lock);
  1052. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1053. if (dma->bufs[i].buf_count)
  1054. ++count;
  1055. }
  1056. DRM_DEBUG("count = %d\n", count);
  1057. if (request->count >= count) {
  1058. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1059. if (dma->bufs[i].buf_count) {
  1060. struct drm_buf_desc __user *to =
  1061. &request->list[count];
  1062. struct drm_buf_entry *from = &dma->bufs[i];
  1063. struct drm_freelist *list = &dma->bufs[i].freelist;
  1064. if (copy_to_user(&to->count,
  1065. &from->buf_count,
  1066. sizeof(from->buf_count)) ||
  1067. copy_to_user(&to->size,
  1068. &from->buf_size,
  1069. sizeof(from->buf_size)) ||
  1070. copy_to_user(&to->low_mark,
  1071. &list->low_mark,
  1072. sizeof(list->low_mark)) ||
  1073. copy_to_user(&to->high_mark,
  1074. &list->high_mark,
  1075. sizeof(list->high_mark)))
  1076. return -EFAULT;
  1077. DRM_DEBUG("%d %d %d %d %d\n",
  1078. i,
  1079. dma->bufs[i].buf_count,
  1080. dma->bufs[i].buf_size,
  1081. dma->bufs[i].freelist.low_mark,
  1082. dma->bufs[i].freelist.high_mark);
  1083. ++count;
  1084. }
  1085. }
  1086. }
  1087. request->count = count;
  1088. return 0;
  1089. }
  1090. /**
  1091. * Specifies a low and high water mark for buffer allocation
  1092. *
  1093. * \param inode device inode.
  1094. * \param file_priv DRM file private.
  1095. * \param cmd command.
  1096. * \param arg a pointer to a drm_buf_desc structure.
  1097. * \return zero on success or a negative number on failure.
  1098. *
  1099. * Verifies that the size order is bounded between the admissible orders and
  1100. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1101. *
  1102. * \note This ioctl is deprecated and mostly never used.
  1103. */
  1104. int drm_markbufs(struct drm_device *dev, void *data,
  1105. struct drm_file *file_priv)
  1106. {
  1107. struct drm_device_dma *dma = dev->dma;
  1108. struct drm_buf_desc *request = data;
  1109. int order;
  1110. struct drm_buf_entry *entry;
  1111. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1112. return -EINVAL;
  1113. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1114. return -EINVAL;
  1115. if (!dma)
  1116. return -EINVAL;
  1117. DRM_DEBUG("%d, %d, %d\n",
  1118. request->size, request->low_mark, request->high_mark);
  1119. order = order_base_2(request->size);
  1120. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1121. return -EINVAL;
  1122. entry = &dma->bufs[order];
  1123. if (request->low_mark < 0 || request->low_mark > entry->buf_count)
  1124. return -EINVAL;
  1125. if (request->high_mark < 0 || request->high_mark > entry->buf_count)
  1126. return -EINVAL;
  1127. entry->freelist.low_mark = request->low_mark;
  1128. entry->freelist.high_mark = request->high_mark;
  1129. return 0;
  1130. }
  1131. /**
  1132. * Unreserve the buffers in list, previously reserved using drmDMA.
  1133. *
  1134. * \param inode device inode.
  1135. * \param file_priv DRM file private.
  1136. * \param cmd command.
  1137. * \param arg pointer to a drm_buf_free structure.
  1138. * \return zero on success or a negative number on failure.
  1139. *
  1140. * Calls free_buffer() for each used buffer.
  1141. * This function is primarily used for debugging.
  1142. */
  1143. int drm_freebufs(struct drm_device *dev, void *data,
  1144. struct drm_file *file_priv)
  1145. {
  1146. struct drm_device_dma *dma = dev->dma;
  1147. struct drm_buf_free *request = data;
  1148. int i;
  1149. int idx;
  1150. struct drm_buf *buf;
  1151. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1152. return -EINVAL;
  1153. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1154. return -EINVAL;
  1155. if (!dma)
  1156. return -EINVAL;
  1157. DRM_DEBUG("%d\n", request->count);
  1158. for (i = 0; i < request->count; i++) {
  1159. if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
  1160. return -EFAULT;
  1161. if (idx < 0 || idx >= dma->buf_count) {
  1162. DRM_ERROR("Index %d (of %d max)\n",
  1163. idx, dma->buf_count - 1);
  1164. return -EINVAL;
  1165. }
  1166. buf = dma->buflist[idx];
  1167. if (buf->file_priv != file_priv) {
  1168. DRM_ERROR("Process %d freeing buffer not owned\n",
  1169. task_pid_nr(current));
  1170. return -EINVAL;
  1171. }
  1172. drm_free_buffer(dev, buf);
  1173. }
  1174. return 0;
  1175. }
  1176. /**
  1177. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1178. *
  1179. * \param inode device inode.
  1180. * \param file_priv DRM file private.
  1181. * \param cmd command.
  1182. * \param arg pointer to a drm_buf_map structure.
  1183. * \return zero on success or a negative number on failure.
  1184. *
  1185. * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
  1186. * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
  1187. * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
  1188. * drm_mmap_dma().
  1189. */
  1190. int drm_mapbufs(struct drm_device *dev, void *data,
  1191. struct drm_file *file_priv)
  1192. {
  1193. struct drm_device_dma *dma = dev->dma;
  1194. int retcode = 0;
  1195. const int zero = 0;
  1196. unsigned long virtual;
  1197. unsigned long address;
  1198. struct drm_buf_map *request = data;
  1199. int i;
  1200. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1201. return -EINVAL;
  1202. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1203. return -EINVAL;
  1204. if (!dma)
  1205. return -EINVAL;
  1206. spin_lock(&dev->buf_lock);
  1207. if (atomic_read(&dev->buf_alloc)) {
  1208. spin_unlock(&dev->buf_lock);
  1209. return -EBUSY;
  1210. }
  1211. dev->buf_use++; /* Can't allocate more after this call */
  1212. spin_unlock(&dev->buf_lock);
  1213. if (request->count >= dma->buf_count) {
  1214. if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
  1215. || (drm_core_check_feature(dev, DRIVER_SG)
  1216. && (dma->flags & _DRM_DMA_USE_SG))) {
  1217. struct drm_local_map *map = dev->agp_buffer_map;
  1218. unsigned long token = dev->agp_buffer_token;
  1219. if (!map) {
  1220. retcode = -EINVAL;
  1221. goto done;
  1222. }
  1223. virtual = vm_mmap(file_priv->filp, 0, map->size,
  1224. PROT_READ | PROT_WRITE,
  1225. MAP_SHARED,
  1226. token);
  1227. } else {
  1228. virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
  1229. PROT_READ | PROT_WRITE,
  1230. MAP_SHARED, 0);
  1231. }
  1232. if (virtual > -1024UL) {
  1233. /* Real error */
  1234. retcode = (signed long)virtual;
  1235. goto done;
  1236. }
  1237. request->virtual = (void __user *)virtual;
  1238. for (i = 0; i < dma->buf_count; i++) {
  1239. if (copy_to_user(&request->list[i].idx,
  1240. &dma->buflist[i]->idx,
  1241. sizeof(request->list[0].idx))) {
  1242. retcode = -EFAULT;
  1243. goto done;
  1244. }
  1245. if (copy_to_user(&request->list[i].total,
  1246. &dma->buflist[i]->total,
  1247. sizeof(request->list[0].total))) {
  1248. retcode = -EFAULT;
  1249. goto done;
  1250. }
  1251. if (copy_to_user(&request->list[i].used,
  1252. &zero, sizeof(zero))) {
  1253. retcode = -EFAULT;
  1254. goto done;
  1255. }
  1256. address = virtual + dma->buflist[i]->offset; /* *** */
  1257. if (copy_to_user(&request->list[i].address,
  1258. &address, sizeof(address))) {
  1259. retcode = -EFAULT;
  1260. goto done;
  1261. }
  1262. }
  1263. }
  1264. done:
  1265. request->count = dma->buf_count;
  1266. DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
  1267. return retcode;
  1268. }
  1269. int drm_dma_ioctl(struct drm_device *dev, void *data,
  1270. struct drm_file *file_priv)
  1271. {
  1272. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1273. return -EINVAL;
  1274. if (dev->driver->dma_ioctl)
  1275. return dev->driver->dma_ioctl(dev, data, file_priv);
  1276. else
  1277. return -EINVAL;
  1278. }
  1279. struct drm_local_map *drm_getsarea(struct drm_device *dev)
  1280. {
  1281. struct drm_map_list *entry;
  1282. list_for_each_entry(entry, &dev->maplist, head) {
  1283. if (entry->map && entry->map->type == _DRM_SHM &&
  1284. (entry->map->flags & _DRM_CONTAINS_LOCK)) {
  1285. return entry->map;
  1286. }
  1287. }
  1288. return NULL;
  1289. }
  1290. EXPORT_SYMBOL(drm_getsarea);