drm_bufs.c 44 KB

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