drm_bufs.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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 (drm_core_has_MTRR(dev)) {
  192. if (map->type == _DRM_FRAME_BUFFER ||
  193. (map->flags & _DRM_WRITE_COMBINING)) {
  194. map->mtrr =
  195. arch_phys_wc_add(map->offset, map->size);
  196. }
  197. }
  198. if (map->type == _DRM_REGISTERS) {
  199. if (map->flags & _DRM_WRITE_COMBINING)
  200. map->handle = ioremap_wc(map->offset,
  201. map->size);
  202. else
  203. map->handle = ioremap(map->offset, map->size);
  204. if (!map->handle) {
  205. kfree(map);
  206. return -ENOMEM;
  207. }
  208. }
  209. break;
  210. case _DRM_SHM:
  211. list = drm_find_matching_map(dev, map);
  212. if (list != NULL) {
  213. if(list->map->size != map->size) {
  214. DRM_DEBUG("Matching maps of type %d with "
  215. "mismatched sizes, (%ld vs %ld)\n",
  216. map->type, map->size, list->map->size);
  217. list->map->size = map->size;
  218. }
  219. kfree(map);
  220. *maplist = list;
  221. return 0;
  222. }
  223. map->handle = vmalloc_user(map->size);
  224. DRM_DEBUG("%lu %d %p\n",
  225. map->size, order_base_2(map->size), map->handle);
  226. if (!map->handle) {
  227. kfree(map);
  228. return -ENOMEM;
  229. }
  230. map->offset = (unsigned long)map->handle;
  231. if (map->flags & _DRM_CONTAINS_LOCK) {
  232. /* Prevent a 2nd X Server from creating a 2nd lock */
  233. if (dev->primary->master->lock.hw_lock != NULL) {
  234. vfree(map->handle);
  235. kfree(map);
  236. return -EBUSY;
  237. }
  238. dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
  239. }
  240. break;
  241. case _DRM_AGP: {
  242. struct drm_agp_mem *entry;
  243. int valid = 0;
  244. if (!drm_core_has_AGP(dev)) {
  245. kfree(map);
  246. return -EINVAL;
  247. }
  248. #ifdef __alpha__
  249. map->offset += dev->hose->mem_space->start;
  250. #endif
  251. /* In some cases (i810 driver), user space may have already
  252. * added the AGP base itself, because dev->agp->base previously
  253. * only got set during AGP enable. So, only add the base
  254. * address if the map's offset isn't already within the
  255. * aperture.
  256. */
  257. if (map->offset < dev->agp->base ||
  258. map->offset > dev->agp->base +
  259. dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
  260. map->offset += dev->agp->base;
  261. }
  262. map->mtrr = dev->agp->agp_mtrr; /* for getmap */
  263. /* This assumes the DRM is in total control of AGP space.
  264. * It's not always the case as AGP can be in the control
  265. * of user space (i.e. i810 driver). So this loop will get
  266. * skipped and we double check that dev->agp->memory is
  267. * actually set as well as being invalid before EPERM'ing
  268. */
  269. list_for_each_entry(entry, &dev->agp->memory, head) {
  270. if ((map->offset >= entry->bound) &&
  271. (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
  272. valid = 1;
  273. break;
  274. }
  275. }
  276. if (!list_empty(&dev->agp->memory) && !valid) {
  277. kfree(map);
  278. return -EPERM;
  279. }
  280. DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
  281. (unsigned long long)map->offset, map->size);
  282. break;
  283. }
  284. case _DRM_GEM:
  285. DRM_ERROR("tried to addmap GEM object\n");
  286. break;
  287. case _DRM_SCATTER_GATHER:
  288. if (!dev->sg) {
  289. kfree(map);
  290. return -EINVAL;
  291. }
  292. map->offset += (unsigned long)dev->sg->virtual;
  293. break;
  294. case _DRM_CONSISTENT:
  295. /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
  296. * As we're limiting the address to 2^32-1 (or less),
  297. * casting it down to 32 bits is no problem, but we
  298. * need to point to a 64bit variable first. */
  299. dmah = drm_pci_alloc(dev, map->size, map->size);
  300. if (!dmah) {
  301. kfree(map);
  302. return -ENOMEM;
  303. }
  304. map->handle = dmah->vaddr;
  305. map->offset = (unsigned long)dmah->busaddr;
  306. kfree(dmah);
  307. break;
  308. default:
  309. kfree(map);
  310. return -EINVAL;
  311. }
  312. list = kzalloc(sizeof(*list), GFP_KERNEL);
  313. if (!list) {
  314. if (map->type == _DRM_REGISTERS)
  315. iounmap(map->handle);
  316. kfree(map);
  317. return -EINVAL;
  318. }
  319. list->map = map;
  320. mutex_lock(&dev->struct_mutex);
  321. list_add(&list->head, &dev->maplist);
  322. /* Assign a 32-bit handle */
  323. /* We do it here so that dev->struct_mutex protects the increment */
  324. user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
  325. map->offset;
  326. ret = drm_map_handle(dev, &list->hash, user_token, 0,
  327. (map->type == _DRM_SHM));
  328. if (ret) {
  329. if (map->type == _DRM_REGISTERS)
  330. iounmap(map->handle);
  331. kfree(map);
  332. kfree(list);
  333. mutex_unlock(&dev->struct_mutex);
  334. return ret;
  335. }
  336. list->user_token = list->hash.key << PAGE_SHIFT;
  337. mutex_unlock(&dev->struct_mutex);
  338. if (!(map->flags & _DRM_DRIVER))
  339. list->master = dev->primary->master;
  340. *maplist = list;
  341. return 0;
  342. }
  343. int drm_addmap(struct drm_device * dev, resource_size_t offset,
  344. unsigned int size, enum drm_map_type type,
  345. enum drm_map_flags flags, struct drm_local_map ** map_ptr)
  346. {
  347. struct drm_map_list *list;
  348. int rc;
  349. rc = drm_addmap_core(dev, offset, size, type, flags, &list);
  350. if (!rc)
  351. *map_ptr = list->map;
  352. return rc;
  353. }
  354. EXPORT_SYMBOL(drm_addmap);
  355. /**
  356. * Ioctl to specify a range of memory that is available for mapping by a
  357. * non-root process.
  358. *
  359. * \param inode device inode.
  360. * \param file_priv DRM file private.
  361. * \param cmd command.
  362. * \param arg pointer to a drm_map structure.
  363. * \return zero on success or a negative value on error.
  364. *
  365. */
  366. int drm_addmap_ioctl(struct drm_device *dev, void *data,
  367. struct drm_file *file_priv)
  368. {
  369. struct drm_map *map = data;
  370. struct drm_map_list *maplist;
  371. int err;
  372. if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
  373. return -EPERM;
  374. err = drm_addmap_core(dev, map->offset, map->size, map->type,
  375. map->flags, &maplist);
  376. if (err)
  377. return err;
  378. /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
  379. map->handle = (void *)(unsigned long)maplist->user_token;
  380. /*
  381. * It appears that there are no users of this value whatsoever --
  382. * drmAddMap just discards it. Let's not encourage its use.
  383. * (Keeping drm_addmap_core's returned mtrr value would be wrong --
  384. * it's not a real mtrr index anymore.)
  385. */
  386. map->mtrr = -1;
  387. return 0;
  388. }
  389. /**
  390. * Remove a map private from list and deallocate resources if the mapping
  391. * isn't in use.
  392. *
  393. * Searches the map on drm_device::maplist, removes it from the list, see if
  394. * its being used, and free any associate resource (such as MTRR's) if it's not
  395. * being on use.
  396. *
  397. * \sa drm_addmap
  398. */
  399. int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
  400. {
  401. struct drm_map_list *r_list = NULL, *list_t;
  402. drm_dma_handle_t dmah;
  403. int found = 0;
  404. struct drm_master *master;
  405. /* Find the list entry for the map and remove it */
  406. list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
  407. if (r_list->map == map) {
  408. master = r_list->master;
  409. list_del(&r_list->head);
  410. drm_ht_remove_key(&dev->map_hash,
  411. r_list->user_token >> PAGE_SHIFT);
  412. kfree(r_list);
  413. found = 1;
  414. break;
  415. }
  416. }
  417. if (!found)
  418. return -EINVAL;
  419. switch (map->type) {
  420. case _DRM_REGISTERS:
  421. iounmap(map->handle);
  422. /* FALLTHROUGH */
  423. case _DRM_FRAME_BUFFER:
  424. if (drm_core_has_MTRR(dev))
  425. arch_phys_wc_del(map->mtrr);
  426. break;
  427. case _DRM_SHM:
  428. vfree(map->handle);
  429. if (master) {
  430. if (dev->sigdata.lock == master->lock.hw_lock)
  431. dev->sigdata.lock = NULL;
  432. master->lock.hw_lock = NULL; /* SHM removed */
  433. master->lock.file_priv = NULL;
  434. wake_up_interruptible_all(&master->lock.lock_queue);
  435. }
  436. break;
  437. case _DRM_AGP:
  438. case _DRM_SCATTER_GATHER:
  439. break;
  440. case _DRM_CONSISTENT:
  441. dmah.vaddr = map->handle;
  442. dmah.busaddr = map->offset;
  443. dmah.size = map->size;
  444. __drm_pci_free(dev, &dmah);
  445. break;
  446. case _DRM_GEM:
  447. DRM_ERROR("tried to rmmap GEM object\n");
  448. break;
  449. }
  450. kfree(map);
  451. return 0;
  452. }
  453. EXPORT_SYMBOL(drm_rmmap_locked);
  454. int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
  455. {
  456. int ret;
  457. mutex_lock(&dev->struct_mutex);
  458. ret = drm_rmmap_locked(dev, map);
  459. mutex_unlock(&dev->struct_mutex);
  460. return ret;
  461. }
  462. EXPORT_SYMBOL(drm_rmmap);
  463. /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
  464. * the last close of the device, and this is necessary for cleanup when things
  465. * exit uncleanly. Therefore, having userland manually remove mappings seems
  466. * like a pointless exercise since they're going away anyway.
  467. *
  468. * One use case might be after addmap is allowed for normal users for SHM and
  469. * gets used by drivers that the server doesn't need to care about. This seems
  470. * unlikely.
  471. *
  472. * \param inode device inode.
  473. * \param file_priv DRM file private.
  474. * \param cmd command.
  475. * \param arg pointer to a struct drm_map structure.
  476. * \return zero on success or a negative value on error.
  477. */
  478. int drm_rmmap_ioctl(struct drm_device *dev, void *data,
  479. struct drm_file *file_priv)
  480. {
  481. struct drm_map *request = data;
  482. struct drm_local_map *map = NULL;
  483. struct drm_map_list *r_list;
  484. int ret;
  485. mutex_lock(&dev->struct_mutex);
  486. list_for_each_entry(r_list, &dev->maplist, head) {
  487. if (r_list->map &&
  488. r_list->user_token == (unsigned long)request->handle &&
  489. r_list->map->flags & _DRM_REMOVABLE) {
  490. map = r_list->map;
  491. break;
  492. }
  493. }
  494. /* List has wrapped around to the head pointer, or its empty we didn't
  495. * find anything.
  496. */
  497. if (list_empty(&dev->maplist) || !map) {
  498. mutex_unlock(&dev->struct_mutex);
  499. return -EINVAL;
  500. }
  501. /* Register and framebuffer maps are permanent */
  502. if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
  503. mutex_unlock(&dev->struct_mutex);
  504. return 0;
  505. }
  506. ret = drm_rmmap_locked(dev, map);
  507. mutex_unlock(&dev->struct_mutex);
  508. return ret;
  509. }
  510. /**
  511. * Cleanup after an error on one of the addbufs() functions.
  512. *
  513. * \param dev DRM device.
  514. * \param entry buffer entry where the error occurred.
  515. *
  516. * Frees any pages and buffers associated with the given entry.
  517. */
  518. static void drm_cleanup_buf_error(struct drm_device * dev,
  519. struct drm_buf_entry * entry)
  520. {
  521. int i;
  522. if (entry->seg_count) {
  523. for (i = 0; i < entry->seg_count; i++) {
  524. if (entry->seglist[i]) {
  525. drm_pci_free(dev, entry->seglist[i]);
  526. }
  527. }
  528. kfree(entry->seglist);
  529. entry->seg_count = 0;
  530. }
  531. if (entry->buf_count) {
  532. for (i = 0; i < entry->buf_count; i++) {
  533. kfree(entry->buflist[i].dev_private);
  534. }
  535. kfree(entry->buflist);
  536. entry->buf_count = 0;
  537. }
  538. }
  539. #if __OS_HAS_AGP
  540. /**
  541. * Add AGP buffers for DMA transfers.
  542. *
  543. * \param dev struct drm_device to which the buffers are to be added.
  544. * \param request pointer to a struct drm_buf_desc describing the request.
  545. * \return zero on success or a negative number on failure.
  546. *
  547. * After some sanity checks creates a drm_buf structure for each buffer and
  548. * reallocates the buffer list of the same size order to accommodate the new
  549. * buffers.
  550. */
  551. int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
  552. {
  553. struct drm_device_dma *dma = dev->dma;
  554. struct drm_buf_entry *entry;
  555. struct drm_agp_mem *agp_entry;
  556. struct drm_buf *buf;
  557. unsigned long offset;
  558. unsigned long agp_offset;
  559. int count;
  560. int order;
  561. int size;
  562. int alignment;
  563. int page_order;
  564. int total;
  565. int byte_count;
  566. int i, valid;
  567. struct drm_buf **temp_buflist;
  568. if (!dma)
  569. return -EINVAL;
  570. count = request->count;
  571. order = order_base_2(request->size);
  572. size = 1 << order;
  573. alignment = (request->flags & _DRM_PAGE_ALIGN)
  574. ? PAGE_ALIGN(size) : size;
  575. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  576. total = PAGE_SIZE << page_order;
  577. byte_count = 0;
  578. agp_offset = dev->agp->base + request->agp_start;
  579. DRM_DEBUG("count: %d\n", count);
  580. DRM_DEBUG("order: %d\n", order);
  581. DRM_DEBUG("size: %d\n", size);
  582. DRM_DEBUG("agp_offset: %lx\n", agp_offset);
  583. DRM_DEBUG("alignment: %d\n", alignment);
  584. DRM_DEBUG("page_order: %d\n", page_order);
  585. DRM_DEBUG("total: %d\n", total);
  586. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  587. return -EINVAL;
  588. /* Make sure buffers are located in AGP memory that we own */
  589. valid = 0;
  590. list_for_each_entry(agp_entry, &dev->agp->memory, head) {
  591. if ((agp_offset >= agp_entry->bound) &&
  592. (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
  593. valid = 1;
  594. break;
  595. }
  596. }
  597. if (!list_empty(&dev->agp->memory) && !valid) {
  598. DRM_DEBUG("zone invalid\n");
  599. return -EINVAL;
  600. }
  601. spin_lock(&dev->count_lock);
  602. if (dev->buf_use) {
  603. spin_unlock(&dev->count_lock);
  604. return -EBUSY;
  605. }
  606. atomic_inc(&dev->buf_alloc);
  607. spin_unlock(&dev->count_lock);
  608. mutex_lock(&dev->struct_mutex);
  609. entry = &dma->bufs[order];
  610. if (entry->buf_count) {
  611. mutex_unlock(&dev->struct_mutex);
  612. atomic_dec(&dev->buf_alloc);
  613. return -ENOMEM; /* May only call once for each order */
  614. }
  615. if (count < 0 || count > 4096) {
  616. mutex_unlock(&dev->struct_mutex);
  617. atomic_dec(&dev->buf_alloc);
  618. return -EINVAL;
  619. }
  620. entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  621. if (!entry->buflist) {
  622. mutex_unlock(&dev->struct_mutex);
  623. atomic_dec(&dev->buf_alloc);
  624. return -ENOMEM;
  625. }
  626. entry->buf_size = size;
  627. entry->page_order = page_order;
  628. offset = 0;
  629. while (entry->buf_count < count) {
  630. buf = &entry->buflist[entry->buf_count];
  631. buf->idx = dma->buf_count + entry->buf_count;
  632. buf->total = alignment;
  633. buf->order = order;
  634. buf->used = 0;
  635. buf->offset = (dma->byte_count + offset);
  636. buf->bus_address = agp_offset + offset;
  637. buf->address = (void *)(agp_offset + offset);
  638. buf->next = NULL;
  639. buf->waiting = 0;
  640. buf->pending = 0;
  641. buf->file_priv = NULL;
  642. buf->dev_priv_size = dev->driver->dev_priv_size;
  643. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  644. if (!buf->dev_private) {
  645. /* Set count correctly so we free the proper amount. */
  646. entry->buf_count = count;
  647. drm_cleanup_buf_error(dev, entry);
  648. mutex_unlock(&dev->struct_mutex);
  649. atomic_dec(&dev->buf_alloc);
  650. return -ENOMEM;
  651. }
  652. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  653. offset += alignment;
  654. entry->buf_count++;
  655. byte_count += PAGE_SIZE << page_order;
  656. }
  657. DRM_DEBUG("byte_count: %d\n", byte_count);
  658. temp_buflist = krealloc(dma->buflist,
  659. (dma->buf_count + entry->buf_count) *
  660. sizeof(*dma->buflist), GFP_KERNEL);
  661. if (!temp_buflist) {
  662. /* Free the entry because it isn't valid */
  663. drm_cleanup_buf_error(dev, entry);
  664. mutex_unlock(&dev->struct_mutex);
  665. atomic_dec(&dev->buf_alloc);
  666. return -ENOMEM;
  667. }
  668. dma->buflist = temp_buflist;
  669. for (i = 0; i < entry->buf_count; i++) {
  670. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  671. }
  672. dma->buf_count += entry->buf_count;
  673. dma->seg_count += entry->seg_count;
  674. dma->page_count += byte_count >> PAGE_SHIFT;
  675. dma->byte_count += byte_count;
  676. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  677. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  678. mutex_unlock(&dev->struct_mutex);
  679. request->count = entry->buf_count;
  680. request->size = size;
  681. dma->flags = _DRM_DMA_USE_AGP;
  682. atomic_dec(&dev->buf_alloc);
  683. return 0;
  684. }
  685. EXPORT_SYMBOL(drm_addbufs_agp);
  686. #endif /* __OS_HAS_AGP */
  687. int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
  688. {
  689. struct drm_device_dma *dma = dev->dma;
  690. int count;
  691. int order;
  692. int size;
  693. int total;
  694. int page_order;
  695. struct drm_buf_entry *entry;
  696. drm_dma_handle_t *dmah;
  697. struct drm_buf *buf;
  698. int alignment;
  699. unsigned long offset;
  700. int i;
  701. int byte_count;
  702. int page_count;
  703. unsigned long *temp_pagelist;
  704. struct drm_buf **temp_buflist;
  705. if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
  706. return -EINVAL;
  707. if (!dma)
  708. return -EINVAL;
  709. if (!capable(CAP_SYS_ADMIN))
  710. return -EPERM;
  711. count = request->count;
  712. order = order_base_2(request->size);
  713. size = 1 << order;
  714. DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
  715. request->count, request->size, size, order);
  716. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  717. return -EINVAL;
  718. alignment = (request->flags & _DRM_PAGE_ALIGN)
  719. ? PAGE_ALIGN(size) : size;
  720. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  721. total = PAGE_SIZE << page_order;
  722. spin_lock(&dev->count_lock);
  723. if (dev->buf_use) {
  724. spin_unlock(&dev->count_lock);
  725. return -EBUSY;
  726. }
  727. atomic_inc(&dev->buf_alloc);
  728. spin_unlock(&dev->count_lock);
  729. mutex_lock(&dev->struct_mutex);
  730. entry = &dma->bufs[order];
  731. if (entry->buf_count) {
  732. mutex_unlock(&dev->struct_mutex);
  733. atomic_dec(&dev->buf_alloc);
  734. return -ENOMEM; /* May only call once for each order */
  735. }
  736. if (count < 0 || count > 4096) {
  737. mutex_unlock(&dev->struct_mutex);
  738. atomic_dec(&dev->buf_alloc);
  739. return -EINVAL;
  740. }
  741. entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
  742. if (!entry->buflist) {
  743. mutex_unlock(&dev->struct_mutex);
  744. atomic_dec(&dev->buf_alloc);
  745. return -ENOMEM;
  746. }
  747. entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
  748. if (!entry->seglist) {
  749. kfree(entry->buflist);
  750. mutex_unlock(&dev->struct_mutex);
  751. atomic_dec(&dev->buf_alloc);
  752. return -ENOMEM;
  753. }
  754. /* Keep the original pagelist until we know all the allocations
  755. * have succeeded
  756. */
  757. temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
  758. sizeof(*dma->pagelist), GFP_KERNEL);
  759. if (!temp_pagelist) {
  760. kfree(entry->buflist);
  761. kfree(entry->seglist);
  762. mutex_unlock(&dev->struct_mutex);
  763. atomic_dec(&dev->buf_alloc);
  764. return -ENOMEM;
  765. }
  766. memcpy(temp_pagelist,
  767. dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
  768. DRM_DEBUG("pagelist: %d entries\n",
  769. dma->page_count + (count << page_order));
  770. entry->buf_size = size;
  771. entry->page_order = page_order;
  772. byte_count = 0;
  773. page_count = 0;
  774. while (entry->buf_count < count) {
  775. dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
  776. if (!dmah) {
  777. /* Set count correctly so we free the proper amount. */
  778. entry->buf_count = count;
  779. entry->seg_count = count;
  780. drm_cleanup_buf_error(dev, entry);
  781. kfree(temp_pagelist);
  782. mutex_unlock(&dev->struct_mutex);
  783. atomic_dec(&dev->buf_alloc);
  784. return -ENOMEM;
  785. }
  786. entry->seglist[entry->seg_count++] = dmah;
  787. for (i = 0; i < (1 << page_order); i++) {
  788. DRM_DEBUG("page %d @ 0x%08lx\n",
  789. dma->page_count + page_count,
  790. (unsigned long)dmah->vaddr + PAGE_SIZE * i);
  791. temp_pagelist[dma->page_count + page_count++]
  792. = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
  793. }
  794. for (offset = 0;
  795. offset + size <= total && entry->buf_count < count;
  796. offset += alignment, ++entry->buf_count) {
  797. buf = &entry->buflist[entry->buf_count];
  798. buf->idx = dma->buf_count + entry->buf_count;
  799. buf->total = alignment;
  800. buf->order = order;
  801. buf->used = 0;
  802. buf->offset = (dma->byte_count + byte_count + offset);
  803. buf->address = (void *)(dmah->vaddr + offset);
  804. buf->bus_address = dmah->busaddr + offset;
  805. buf->next = NULL;
  806. buf->waiting = 0;
  807. buf->pending = 0;
  808. buf->file_priv = NULL;
  809. buf->dev_priv_size = dev->driver->dev_priv_size;
  810. buf->dev_private = kzalloc(buf->dev_priv_size,
  811. GFP_KERNEL);
  812. if (!buf->dev_private) {
  813. /* Set count correctly so we free the proper amount. */
  814. entry->buf_count = count;
  815. entry->seg_count = count;
  816. drm_cleanup_buf_error(dev, entry);
  817. kfree(temp_pagelist);
  818. mutex_unlock(&dev->struct_mutex);
  819. atomic_dec(&dev->buf_alloc);
  820. return -ENOMEM;
  821. }
  822. DRM_DEBUG("buffer %d @ %p\n",
  823. entry->buf_count, buf->address);
  824. }
  825. byte_count += PAGE_SIZE << page_order;
  826. }
  827. temp_buflist = krealloc(dma->buflist,
  828. (dma->buf_count + entry->buf_count) *
  829. sizeof(*dma->buflist), GFP_KERNEL);
  830. if (!temp_buflist) {
  831. /* Free the entry because it isn't valid */
  832. drm_cleanup_buf_error(dev, entry);
  833. kfree(temp_pagelist);
  834. mutex_unlock(&dev->struct_mutex);
  835. atomic_dec(&dev->buf_alloc);
  836. return -ENOMEM;
  837. }
  838. dma->buflist = temp_buflist;
  839. for (i = 0; i < entry->buf_count; i++) {
  840. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  841. }
  842. /* No allocations failed, so now we can replace the original pagelist
  843. * with the new one.
  844. */
  845. if (dma->page_count) {
  846. kfree(dma->pagelist);
  847. }
  848. dma->pagelist = temp_pagelist;
  849. dma->buf_count += entry->buf_count;
  850. dma->seg_count += entry->seg_count;
  851. dma->page_count += entry->seg_count << page_order;
  852. dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
  853. mutex_unlock(&dev->struct_mutex);
  854. request->count = entry->buf_count;
  855. request->size = size;
  856. if (request->flags & _DRM_PCI_BUFFER_RO)
  857. dma->flags = _DRM_DMA_USE_PCI_RO;
  858. atomic_dec(&dev->buf_alloc);
  859. return 0;
  860. }
  861. EXPORT_SYMBOL(drm_addbufs_pci);
  862. static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
  863. {
  864. struct drm_device_dma *dma = dev->dma;
  865. struct drm_buf_entry *entry;
  866. struct drm_buf *buf;
  867. unsigned long offset;
  868. unsigned long agp_offset;
  869. int count;
  870. int order;
  871. int size;
  872. int alignment;
  873. int page_order;
  874. int total;
  875. int byte_count;
  876. int i;
  877. struct drm_buf **temp_buflist;
  878. if (!drm_core_check_feature(dev, DRIVER_SG))
  879. return -EINVAL;
  880. if (!dma)
  881. return -EINVAL;
  882. if (!capable(CAP_SYS_ADMIN))
  883. return -EPERM;
  884. count = request->count;
  885. order = order_base_2(request->size);
  886. size = 1 << order;
  887. alignment = (request->flags & _DRM_PAGE_ALIGN)
  888. ? PAGE_ALIGN(size) : size;
  889. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  890. total = PAGE_SIZE << page_order;
  891. byte_count = 0;
  892. agp_offset = request->agp_start;
  893. DRM_DEBUG("count: %d\n", count);
  894. DRM_DEBUG("order: %d\n", order);
  895. DRM_DEBUG("size: %d\n", size);
  896. DRM_DEBUG("agp_offset: %lu\n", agp_offset);
  897. DRM_DEBUG("alignment: %d\n", alignment);
  898. DRM_DEBUG("page_order: %d\n", page_order);
  899. DRM_DEBUG("total: %d\n", total);
  900. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  901. return -EINVAL;
  902. spin_lock(&dev->count_lock);
  903. if (dev->buf_use) {
  904. spin_unlock(&dev->count_lock);
  905. return -EBUSY;
  906. }
  907. atomic_inc(&dev->buf_alloc);
  908. spin_unlock(&dev->count_lock);
  909. mutex_lock(&dev->struct_mutex);
  910. entry = &dma->bufs[order];
  911. if (entry->buf_count) {
  912. mutex_unlock(&dev->struct_mutex);
  913. atomic_dec(&dev->buf_alloc);
  914. return -ENOMEM; /* May only call once for each order */
  915. }
  916. if (count < 0 || count > 4096) {
  917. mutex_unlock(&dev->struct_mutex);
  918. atomic_dec(&dev->buf_alloc);
  919. return -EINVAL;
  920. }
  921. entry->buflist = kzalloc(count * sizeof(*entry->buflist),
  922. GFP_KERNEL);
  923. if (!entry->buflist) {
  924. mutex_unlock(&dev->struct_mutex);
  925. atomic_dec(&dev->buf_alloc);
  926. return -ENOMEM;
  927. }
  928. entry->buf_size = size;
  929. entry->page_order = page_order;
  930. offset = 0;
  931. while (entry->buf_count < count) {
  932. buf = &entry->buflist[entry->buf_count];
  933. buf->idx = dma->buf_count + entry->buf_count;
  934. buf->total = alignment;
  935. buf->order = order;
  936. buf->used = 0;
  937. buf->offset = (dma->byte_count + offset);
  938. buf->bus_address = agp_offset + offset;
  939. buf->address = (void *)(agp_offset + offset
  940. + (unsigned long)dev->sg->virtual);
  941. buf->next = NULL;
  942. buf->waiting = 0;
  943. buf->pending = 0;
  944. buf->file_priv = NULL;
  945. buf->dev_priv_size = dev->driver->dev_priv_size;
  946. buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
  947. if (!buf->dev_private) {
  948. /* Set count correctly so we free the proper amount. */
  949. entry->buf_count = count;
  950. drm_cleanup_buf_error(dev, entry);
  951. mutex_unlock(&dev->struct_mutex);
  952. atomic_dec(&dev->buf_alloc);
  953. return -ENOMEM;
  954. }
  955. DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
  956. offset += alignment;
  957. entry->buf_count++;
  958. byte_count += PAGE_SIZE << page_order;
  959. }
  960. DRM_DEBUG("byte_count: %d\n", byte_count);
  961. temp_buflist = krealloc(dma->buflist,
  962. (dma->buf_count + entry->buf_count) *
  963. sizeof(*dma->buflist), GFP_KERNEL);
  964. if (!temp_buflist) {
  965. /* Free the entry because it isn't valid */
  966. drm_cleanup_buf_error(dev, entry);
  967. mutex_unlock(&dev->struct_mutex);
  968. atomic_dec(&dev->buf_alloc);
  969. return -ENOMEM;
  970. }
  971. dma->buflist = temp_buflist;
  972. for (i = 0; i < entry->buf_count; i++) {
  973. dma->buflist[i + dma->buf_count] = &entry->buflist[i];
  974. }
  975. dma->buf_count += entry->buf_count;
  976. dma->seg_count += entry->seg_count;
  977. dma->page_count += byte_count >> PAGE_SHIFT;
  978. dma->byte_count += byte_count;
  979. DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
  980. DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
  981. mutex_unlock(&dev->struct_mutex);
  982. request->count = entry->buf_count;
  983. request->size = size;
  984. dma->flags = _DRM_DMA_USE_SG;
  985. atomic_dec(&dev->buf_alloc);
  986. return 0;
  987. }
  988. /**
  989. * Add buffers for DMA transfers (ioctl).
  990. *
  991. * \param inode device inode.
  992. * \param file_priv DRM file private.
  993. * \param cmd command.
  994. * \param arg pointer to a struct drm_buf_desc request.
  995. * \return zero on success or a negative number on failure.
  996. *
  997. * According with the memory type specified in drm_buf_desc::flags and the
  998. * build options, it dispatches the call either to addbufs_agp(),
  999. * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
  1000. * PCI memory respectively.
  1001. */
  1002. int drm_addbufs(struct drm_device *dev, void *data,
  1003. struct drm_file *file_priv)
  1004. {
  1005. struct drm_buf_desc *request = data;
  1006. int ret;
  1007. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1008. return -EINVAL;
  1009. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1010. return -EINVAL;
  1011. #if __OS_HAS_AGP
  1012. if (request->flags & _DRM_AGP_BUFFER)
  1013. ret = drm_addbufs_agp(dev, request);
  1014. else
  1015. #endif
  1016. if (request->flags & _DRM_SG_BUFFER)
  1017. ret = drm_addbufs_sg(dev, request);
  1018. else if (request->flags & _DRM_FB_BUFFER)
  1019. ret = -EINVAL;
  1020. else
  1021. ret = drm_addbufs_pci(dev, request);
  1022. return ret;
  1023. }
  1024. /**
  1025. * Get information about the buffer mappings.
  1026. *
  1027. * This was originally mean for debugging purposes, or by a sophisticated
  1028. * client library to determine how best to use the available buffers (e.g.,
  1029. * large buffers can be used for image transfer).
  1030. *
  1031. * \param inode device inode.
  1032. * \param file_priv DRM file private.
  1033. * \param cmd command.
  1034. * \param arg pointer to a drm_buf_info structure.
  1035. * \return zero on success or a negative number on failure.
  1036. *
  1037. * Increments drm_device::buf_use while holding the drm_device::count_lock
  1038. * lock, preventing of allocating more buffers after this call. Information
  1039. * about each requested buffer is then copied into user space.
  1040. */
  1041. int drm_infobufs(struct drm_device *dev, void *data,
  1042. struct drm_file *file_priv)
  1043. {
  1044. struct drm_device_dma *dma = dev->dma;
  1045. struct drm_buf_info *request = data;
  1046. int i;
  1047. int count;
  1048. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1049. return -EINVAL;
  1050. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1051. return -EINVAL;
  1052. if (!dma)
  1053. return -EINVAL;
  1054. spin_lock(&dev->count_lock);
  1055. if (atomic_read(&dev->buf_alloc)) {
  1056. spin_unlock(&dev->count_lock);
  1057. return -EBUSY;
  1058. }
  1059. ++dev->buf_use; /* Can't allocate more after this call */
  1060. spin_unlock(&dev->count_lock);
  1061. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1062. if (dma->bufs[i].buf_count)
  1063. ++count;
  1064. }
  1065. DRM_DEBUG("count = %d\n", count);
  1066. if (request->count >= count) {
  1067. for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
  1068. if (dma->bufs[i].buf_count) {
  1069. struct drm_buf_desc __user *to =
  1070. &request->list[count];
  1071. struct drm_buf_entry *from = &dma->bufs[i];
  1072. struct drm_freelist *list = &dma->bufs[i].freelist;
  1073. if (copy_to_user(&to->count,
  1074. &from->buf_count,
  1075. sizeof(from->buf_count)) ||
  1076. copy_to_user(&to->size,
  1077. &from->buf_size,
  1078. sizeof(from->buf_size)) ||
  1079. copy_to_user(&to->low_mark,
  1080. &list->low_mark,
  1081. sizeof(list->low_mark)) ||
  1082. copy_to_user(&to->high_mark,
  1083. &list->high_mark,
  1084. sizeof(list->high_mark)))
  1085. return -EFAULT;
  1086. DRM_DEBUG("%d %d %d %d %d\n",
  1087. i,
  1088. dma->bufs[i].buf_count,
  1089. dma->bufs[i].buf_size,
  1090. dma->bufs[i].freelist.low_mark,
  1091. dma->bufs[i].freelist.high_mark);
  1092. ++count;
  1093. }
  1094. }
  1095. }
  1096. request->count = count;
  1097. return 0;
  1098. }
  1099. /**
  1100. * Specifies a low and high water mark for buffer allocation
  1101. *
  1102. * \param inode device inode.
  1103. * \param file_priv DRM file private.
  1104. * \param cmd command.
  1105. * \param arg a pointer to a drm_buf_desc structure.
  1106. * \return zero on success or a negative number on failure.
  1107. *
  1108. * Verifies that the size order is bounded between the admissible orders and
  1109. * updates the respective drm_device_dma::bufs entry low and high water mark.
  1110. *
  1111. * \note This ioctl is deprecated and mostly never used.
  1112. */
  1113. int drm_markbufs(struct drm_device *dev, void *data,
  1114. struct drm_file *file_priv)
  1115. {
  1116. struct drm_device_dma *dma = dev->dma;
  1117. struct drm_buf_desc *request = data;
  1118. int order;
  1119. struct drm_buf_entry *entry;
  1120. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1121. return -EINVAL;
  1122. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1123. return -EINVAL;
  1124. if (!dma)
  1125. return -EINVAL;
  1126. DRM_DEBUG("%d, %d, %d\n",
  1127. request->size, request->low_mark, request->high_mark);
  1128. order = order_base_2(request->size);
  1129. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
  1130. return -EINVAL;
  1131. entry = &dma->bufs[order];
  1132. if (request->low_mark < 0 || request->low_mark > entry->buf_count)
  1133. return -EINVAL;
  1134. if (request->high_mark < 0 || request->high_mark > entry->buf_count)
  1135. return -EINVAL;
  1136. entry->freelist.low_mark = request->low_mark;
  1137. entry->freelist.high_mark = request->high_mark;
  1138. return 0;
  1139. }
  1140. /**
  1141. * Unreserve the buffers in list, previously reserved using drmDMA.
  1142. *
  1143. * \param inode device inode.
  1144. * \param file_priv DRM file private.
  1145. * \param cmd command.
  1146. * \param arg pointer to a drm_buf_free structure.
  1147. * \return zero on success or a negative number on failure.
  1148. *
  1149. * Calls free_buffer() for each used buffer.
  1150. * This function is primarily used for debugging.
  1151. */
  1152. int drm_freebufs(struct drm_device *dev, void *data,
  1153. struct drm_file *file_priv)
  1154. {
  1155. struct drm_device_dma *dma = dev->dma;
  1156. struct drm_buf_free *request = data;
  1157. int i;
  1158. int idx;
  1159. struct drm_buf *buf;
  1160. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1161. return -EINVAL;
  1162. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1163. return -EINVAL;
  1164. if (!dma)
  1165. return -EINVAL;
  1166. DRM_DEBUG("%d\n", request->count);
  1167. for (i = 0; i < request->count; i++) {
  1168. if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
  1169. return -EFAULT;
  1170. if (idx < 0 || idx >= dma->buf_count) {
  1171. DRM_ERROR("Index %d (of %d max)\n",
  1172. idx, dma->buf_count - 1);
  1173. return -EINVAL;
  1174. }
  1175. buf = dma->buflist[idx];
  1176. if (buf->file_priv != file_priv) {
  1177. DRM_ERROR("Process %d freeing buffer not owned\n",
  1178. task_pid_nr(current));
  1179. return -EINVAL;
  1180. }
  1181. drm_free_buffer(dev, buf);
  1182. }
  1183. return 0;
  1184. }
  1185. /**
  1186. * Maps all of the DMA buffers into client-virtual space (ioctl).
  1187. *
  1188. * \param inode device inode.
  1189. * \param file_priv DRM file private.
  1190. * \param cmd command.
  1191. * \param arg pointer to a drm_buf_map structure.
  1192. * \return zero on success or a negative number on failure.
  1193. *
  1194. * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
  1195. * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
  1196. * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
  1197. * drm_mmap_dma().
  1198. */
  1199. int drm_mapbufs(struct drm_device *dev, void *data,
  1200. struct drm_file *file_priv)
  1201. {
  1202. struct drm_device_dma *dma = dev->dma;
  1203. int retcode = 0;
  1204. const int zero = 0;
  1205. unsigned long virtual;
  1206. unsigned long address;
  1207. struct drm_buf_map *request = data;
  1208. int i;
  1209. if (drm_core_check_feature(dev, DRIVER_MODESET))
  1210. return -EINVAL;
  1211. if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  1212. return -EINVAL;
  1213. if (!dma)
  1214. return -EINVAL;
  1215. spin_lock(&dev->count_lock);
  1216. if (atomic_read(&dev->buf_alloc)) {
  1217. spin_unlock(&dev->count_lock);
  1218. return -EBUSY;
  1219. }
  1220. dev->buf_use++; /* Can't allocate more after this call */
  1221. spin_unlock(&dev->count_lock);
  1222. if (request->count >= dma->buf_count) {
  1223. if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
  1224. || (drm_core_check_feature(dev, DRIVER_SG)
  1225. && (dma->flags & _DRM_DMA_USE_SG))) {
  1226. struct drm_local_map *map = dev->agp_buffer_map;
  1227. unsigned long token = dev->agp_buffer_token;
  1228. if (!map) {
  1229. retcode = -EINVAL;
  1230. goto done;
  1231. }
  1232. virtual = vm_mmap(file_priv->filp, 0, map->size,
  1233. PROT_READ | PROT_WRITE,
  1234. MAP_SHARED,
  1235. token);
  1236. } else {
  1237. virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
  1238. PROT_READ | PROT_WRITE,
  1239. MAP_SHARED, 0);
  1240. }
  1241. if (virtual > -1024UL) {
  1242. /* Real error */
  1243. retcode = (signed long)virtual;
  1244. goto done;
  1245. }
  1246. request->virtual = (void __user *)virtual;
  1247. for (i = 0; i < dma->buf_count; i++) {
  1248. if (copy_to_user(&request->list[i].idx,
  1249. &dma->buflist[i]->idx,
  1250. sizeof(request->list[0].idx))) {
  1251. retcode = -EFAULT;
  1252. goto done;
  1253. }
  1254. if (copy_to_user(&request->list[i].total,
  1255. &dma->buflist[i]->total,
  1256. sizeof(request->list[0].total))) {
  1257. retcode = -EFAULT;
  1258. goto done;
  1259. }
  1260. if (copy_to_user(&request->list[i].used,
  1261. &zero, sizeof(zero))) {
  1262. retcode = -EFAULT;
  1263. goto done;
  1264. }
  1265. address = virtual + dma->buflist[i]->offset; /* *** */
  1266. if (copy_to_user(&request->list[i].address,
  1267. &address, sizeof(address))) {
  1268. retcode = -EFAULT;
  1269. goto done;
  1270. }
  1271. }
  1272. }
  1273. done:
  1274. request->count = dma->buf_count;
  1275. DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
  1276. return retcode;
  1277. }
  1278. struct drm_local_map *drm_getsarea(struct drm_device *dev)
  1279. {
  1280. struct drm_map_list *entry;
  1281. list_for_each_entry(entry, &dev->maplist, head) {
  1282. if (entry->map && entry->map->type == _DRM_SHM &&
  1283. (entry->map->flags & _DRM_CONTAINS_LOCK)) {
  1284. return entry->map;
  1285. }
  1286. }
  1287. return NULL;
  1288. }
  1289. EXPORT_SYMBOL(drm_getsarea);