drm_bufs.c 42 KB

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