drm_bufs.c 42 KB

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