drm_bufs.c 39 KB

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