drm_bufs.c 42 KB

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