drm_bufs.c 39 KB

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