drm_bufs.c 41 KB

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