i810_dma.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. /* i810_dma.c -- DMA support for the i810 -*- linux-c -*-
  2. * Created: Mon Dec 13 01:50:01 1999 by jhartmann@precisioninsight.com
  3. *
  4. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  5. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  6. * All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the next
  16. * paragraph) shall be included in all copies or substantial portions of the
  17. * Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. *
  27. * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
  28. * Jeff Hartmann <jhartmann@valinux.com>
  29. * Keith Whitwell <keith@tungstengraphics.com>
  30. *
  31. */
  32. #include "drmP.h"
  33. #include "drm.h"
  34. #include "i810_drm.h"
  35. #include "i810_drv.h"
  36. #include <linux/interrupt.h> /* For task queue support */
  37. #include <linux/delay.h>
  38. #include <linux/pagemap.h>
  39. #define I810_BUF_FREE 2
  40. #define I810_BUF_CLIENT 1
  41. #define I810_BUF_HARDWARE 0
  42. #define I810_BUF_UNMAPPED 0
  43. #define I810_BUF_MAPPED 1
  44. static struct drm_buf *i810_freelist_get(struct drm_device * dev)
  45. {
  46. struct drm_device_dma *dma = dev->dma;
  47. int i;
  48. int used;
  49. /* Linear search might not be the best solution */
  50. for (i = 0; i < dma->buf_count; i++) {
  51. struct drm_buf *buf = dma->buflist[i];
  52. drm_i810_buf_priv_t *buf_priv = buf->dev_private;
  53. /* In use is already a pointer */
  54. used = cmpxchg(buf_priv->in_use, I810_BUF_FREE,
  55. I810_BUF_CLIENT);
  56. if (used == I810_BUF_FREE) {
  57. return buf;
  58. }
  59. }
  60. return NULL;
  61. }
  62. /* This should only be called if the buffer is not sent to the hardware
  63. * yet, the hardware updates in use for us once its on the ring buffer.
  64. */
  65. static int i810_freelist_put(struct drm_device * dev, struct drm_buf * buf)
  66. {
  67. drm_i810_buf_priv_t *buf_priv = buf->dev_private;
  68. int used;
  69. /* In use is already a pointer */
  70. used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_FREE);
  71. if (used != I810_BUF_CLIENT) {
  72. DRM_ERROR("Freeing buffer thats not in use : %d\n", buf->idx);
  73. return -EINVAL;
  74. }
  75. return 0;
  76. }
  77. static int i810_mmap_buffers(struct file *filp, struct vm_area_struct *vma)
  78. {
  79. struct drm_file *priv = filp->private_data;
  80. struct drm_device *dev;
  81. drm_i810_private_t *dev_priv;
  82. struct drm_buf *buf;
  83. drm_i810_buf_priv_t *buf_priv;
  84. lock_kernel();
  85. dev = priv->head->dev;
  86. dev_priv = dev->dev_private;
  87. buf = dev_priv->mmap_buffer;
  88. buf_priv = buf->dev_private;
  89. vma->vm_flags |= (VM_IO | VM_DONTCOPY);
  90. vma->vm_file = filp;
  91. buf_priv->currently_mapped = I810_BUF_MAPPED;
  92. unlock_kernel();
  93. if (io_remap_pfn_range(vma, vma->vm_start,
  94. vma->vm_pgoff,
  95. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  96. return -EAGAIN;
  97. return 0;
  98. }
  99. static const struct file_operations i810_buffer_fops = {
  100. .open = drm_open,
  101. .release = drm_release,
  102. .ioctl = drm_ioctl,
  103. .mmap = i810_mmap_buffers,
  104. .fasync = drm_fasync,
  105. };
  106. static int i810_map_buffer(struct drm_buf * buf, struct file *filp)
  107. {
  108. struct drm_file *priv = filp->private_data;
  109. struct drm_device *dev = priv->head->dev;
  110. drm_i810_buf_priv_t *buf_priv = buf->dev_private;
  111. drm_i810_private_t *dev_priv = dev->dev_private;
  112. const struct file_operations *old_fops;
  113. int retcode = 0;
  114. if (buf_priv->currently_mapped == I810_BUF_MAPPED)
  115. return -EINVAL;
  116. down_write(&current->mm->mmap_sem);
  117. old_fops = filp->f_op;
  118. filp->f_op = &i810_buffer_fops;
  119. dev_priv->mmap_buffer = buf;
  120. buf_priv->virtual = (void *)do_mmap(filp, 0, buf->total,
  121. PROT_READ | PROT_WRITE,
  122. MAP_SHARED, buf->bus_address);
  123. dev_priv->mmap_buffer = NULL;
  124. filp->f_op = old_fops;
  125. if (IS_ERR(buf_priv->virtual)) {
  126. /* Real error */
  127. DRM_ERROR("mmap error\n");
  128. retcode = PTR_ERR(buf_priv->virtual);
  129. buf_priv->virtual = NULL;
  130. }
  131. up_write(&current->mm->mmap_sem);
  132. return retcode;
  133. }
  134. static int i810_unmap_buffer(struct drm_buf * buf)
  135. {
  136. drm_i810_buf_priv_t *buf_priv = buf->dev_private;
  137. int retcode = 0;
  138. if (buf_priv->currently_mapped != I810_BUF_MAPPED)
  139. return -EINVAL;
  140. down_write(&current->mm->mmap_sem);
  141. retcode = do_munmap(current->mm,
  142. (unsigned long)buf_priv->virtual,
  143. (size_t) buf->total);
  144. up_write(&current->mm->mmap_sem);
  145. buf_priv->currently_mapped = I810_BUF_UNMAPPED;
  146. buf_priv->virtual = NULL;
  147. return retcode;
  148. }
  149. static int i810_dma_get_buffer(struct drm_device * dev, drm_i810_dma_t * d,
  150. struct file *filp)
  151. {
  152. struct drm_buf *buf;
  153. drm_i810_buf_priv_t *buf_priv;
  154. int retcode = 0;
  155. buf = i810_freelist_get(dev);
  156. if (!buf) {
  157. retcode = -ENOMEM;
  158. DRM_DEBUG("retcode=%d\n", retcode);
  159. return retcode;
  160. }
  161. retcode = i810_map_buffer(buf, filp);
  162. if (retcode) {
  163. i810_freelist_put(dev, buf);
  164. DRM_ERROR("mapbuf failed, retcode %d\n", retcode);
  165. return retcode;
  166. }
  167. buf->filp = filp;
  168. buf_priv = buf->dev_private;
  169. d->granted = 1;
  170. d->request_idx = buf->idx;
  171. d->request_size = buf->total;
  172. d->virtual = buf_priv->virtual;
  173. return retcode;
  174. }
  175. static int i810_dma_cleanup(struct drm_device * dev)
  176. {
  177. struct drm_device_dma *dma = dev->dma;
  178. /* Make sure interrupts are disabled here because the uninstall ioctl
  179. * may not have been called from userspace and after dev_private
  180. * is freed, it's too late.
  181. */
  182. if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ) && dev->irq_enabled)
  183. drm_irq_uninstall(dev);
  184. if (dev->dev_private) {
  185. int i;
  186. drm_i810_private_t *dev_priv =
  187. (drm_i810_private_t *) dev->dev_private;
  188. if (dev_priv->ring.virtual_start) {
  189. drm_core_ioremapfree(&dev_priv->ring.map, dev);
  190. }
  191. if (dev_priv->hw_status_page) {
  192. pci_free_consistent(dev->pdev, PAGE_SIZE,
  193. dev_priv->hw_status_page,
  194. dev_priv->dma_status_page);
  195. /* Need to rewrite hardware status page */
  196. I810_WRITE(0x02080, 0x1ffff000);
  197. }
  198. drm_free(dev->dev_private, sizeof(drm_i810_private_t),
  199. DRM_MEM_DRIVER);
  200. dev->dev_private = NULL;
  201. for (i = 0; i < dma->buf_count; i++) {
  202. struct drm_buf *buf = dma->buflist[i];
  203. drm_i810_buf_priv_t *buf_priv = buf->dev_private;
  204. if (buf_priv->kernel_virtual && buf->total)
  205. drm_core_ioremapfree(&buf_priv->map, dev);
  206. }
  207. }
  208. return 0;
  209. }
  210. static int i810_wait_ring(struct drm_device * dev, int n)
  211. {
  212. drm_i810_private_t *dev_priv = dev->dev_private;
  213. drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
  214. int iters = 0;
  215. unsigned long end;
  216. unsigned int last_head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
  217. end = jiffies + (HZ * 3);
  218. while (ring->space < n) {
  219. ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
  220. ring->space = ring->head - (ring->tail + 8);
  221. if (ring->space < 0)
  222. ring->space += ring->Size;
  223. if (ring->head != last_head) {
  224. end = jiffies + (HZ * 3);
  225. last_head = ring->head;
  226. }
  227. iters++;
  228. if (time_before(end, jiffies)) {
  229. DRM_ERROR("space: %d wanted %d\n", ring->space, n);
  230. DRM_ERROR("lockup\n");
  231. goto out_wait_ring;
  232. }
  233. udelay(1);
  234. }
  235. out_wait_ring:
  236. return iters;
  237. }
  238. static void i810_kernel_lost_context(struct drm_device * dev)
  239. {
  240. drm_i810_private_t *dev_priv = dev->dev_private;
  241. drm_i810_ring_buffer_t *ring = &(dev_priv->ring);
  242. ring->head = I810_READ(LP_RING + RING_HEAD) & HEAD_ADDR;
  243. ring->tail = I810_READ(LP_RING + RING_TAIL);
  244. ring->space = ring->head - (ring->tail + 8);
  245. if (ring->space < 0)
  246. ring->space += ring->Size;
  247. }
  248. static int i810_freelist_init(struct drm_device * dev, drm_i810_private_t * dev_priv)
  249. {
  250. struct drm_device_dma *dma = dev->dma;
  251. int my_idx = 24;
  252. u32 *hw_status = (u32 *) (dev_priv->hw_status_page + my_idx);
  253. int i;
  254. if (dma->buf_count > 1019) {
  255. /* Not enough space in the status page for the freelist */
  256. return -EINVAL;
  257. }
  258. for (i = 0; i < dma->buf_count; i++) {
  259. struct drm_buf *buf = dma->buflist[i];
  260. drm_i810_buf_priv_t *buf_priv = buf->dev_private;
  261. buf_priv->in_use = hw_status++;
  262. buf_priv->my_use_idx = my_idx;
  263. my_idx += 4;
  264. *buf_priv->in_use = I810_BUF_FREE;
  265. buf_priv->map.offset = buf->bus_address;
  266. buf_priv->map.size = buf->total;
  267. buf_priv->map.type = _DRM_AGP;
  268. buf_priv->map.flags = 0;
  269. buf_priv->map.mtrr = 0;
  270. drm_core_ioremap(&buf_priv->map, dev);
  271. buf_priv->kernel_virtual = buf_priv->map.handle;
  272. }
  273. return 0;
  274. }
  275. static int i810_dma_initialize(struct drm_device * dev,
  276. drm_i810_private_t * dev_priv,
  277. drm_i810_init_t * init)
  278. {
  279. struct drm_map_list *r_list;
  280. memset(dev_priv, 0, sizeof(drm_i810_private_t));
  281. list_for_each_entry(r_list, &dev->maplist, head) {
  282. if (r_list->map &&
  283. r_list->map->type == _DRM_SHM &&
  284. r_list->map->flags & _DRM_CONTAINS_LOCK) {
  285. dev_priv->sarea_map = r_list->map;
  286. break;
  287. }
  288. }
  289. if (!dev_priv->sarea_map) {
  290. dev->dev_private = (void *)dev_priv;
  291. i810_dma_cleanup(dev);
  292. DRM_ERROR("can not find sarea!\n");
  293. return -EINVAL;
  294. }
  295. dev_priv->mmio_map = drm_core_findmap(dev, init->mmio_offset);
  296. if (!dev_priv->mmio_map) {
  297. dev->dev_private = (void *)dev_priv;
  298. i810_dma_cleanup(dev);
  299. DRM_ERROR("can not find mmio map!\n");
  300. return -EINVAL;
  301. }
  302. dev->agp_buffer_token = init->buffers_offset;
  303. dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
  304. if (!dev->agp_buffer_map) {
  305. dev->dev_private = (void *)dev_priv;
  306. i810_dma_cleanup(dev);
  307. DRM_ERROR("can not find dma buffer map!\n");
  308. return -EINVAL;
  309. }
  310. dev_priv->sarea_priv = (drm_i810_sarea_t *)
  311. ((u8 *) dev_priv->sarea_map->handle + init->sarea_priv_offset);
  312. dev_priv->ring.Start = init->ring_start;
  313. dev_priv->ring.End = init->ring_end;
  314. dev_priv->ring.Size = init->ring_size;
  315. dev_priv->ring.map.offset = dev->agp->base + init->ring_start;
  316. dev_priv->ring.map.size = init->ring_size;
  317. dev_priv->ring.map.type = _DRM_AGP;
  318. dev_priv->ring.map.flags = 0;
  319. dev_priv->ring.map.mtrr = 0;
  320. drm_core_ioremap(&dev_priv->ring.map, dev);
  321. if (dev_priv->ring.map.handle == NULL) {
  322. dev->dev_private = (void *)dev_priv;
  323. i810_dma_cleanup(dev);
  324. DRM_ERROR("can not ioremap virtual address for"
  325. " ring buffer\n");
  326. return DRM_ERR(ENOMEM);
  327. }
  328. dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
  329. dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
  330. dev_priv->w = init->w;
  331. dev_priv->h = init->h;
  332. dev_priv->pitch = init->pitch;
  333. dev_priv->back_offset = init->back_offset;
  334. dev_priv->depth_offset = init->depth_offset;
  335. dev_priv->front_offset = init->front_offset;
  336. dev_priv->overlay_offset = init->overlay_offset;
  337. dev_priv->overlay_physical = init->overlay_physical;
  338. dev_priv->front_di1 = init->front_offset | init->pitch_bits;
  339. dev_priv->back_di1 = init->back_offset | init->pitch_bits;
  340. dev_priv->zi1 = init->depth_offset | init->pitch_bits;
  341. /* Program Hardware Status Page */
  342. dev_priv->hw_status_page =
  343. pci_alloc_consistent(dev->pdev, PAGE_SIZE,
  344. &dev_priv->dma_status_page);
  345. if (!dev_priv->hw_status_page) {
  346. dev->dev_private = (void *)dev_priv;
  347. i810_dma_cleanup(dev);
  348. DRM_ERROR("Can not allocate hardware status page\n");
  349. return -ENOMEM;
  350. }
  351. memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
  352. DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
  353. I810_WRITE(0x02080, dev_priv->dma_status_page);
  354. DRM_DEBUG("Enabled hardware status page\n");
  355. /* Now we need to init our freelist */
  356. if (i810_freelist_init(dev, dev_priv) != 0) {
  357. dev->dev_private = (void *)dev_priv;
  358. i810_dma_cleanup(dev);
  359. DRM_ERROR("Not enough space in the status page for"
  360. " the freelist\n");
  361. return -ENOMEM;
  362. }
  363. dev->dev_private = (void *)dev_priv;
  364. return 0;
  365. }
  366. /* i810 DRM version 1.1 used a smaller init structure with different
  367. * ordering of values than is currently used (drm >= 1.2). There is
  368. * no defined way to detect the XFree version to correct this problem,
  369. * however by checking using this procedure we can detect the correct
  370. * thing to do.
  371. *
  372. * #1 Read the Smaller init structure from user-space
  373. * #2 Verify the overlay_physical is a valid physical address, or NULL
  374. * If it isn't then we have a v1.1 client. Fix up params.
  375. * If it is, then we have a 1.2 client... get the rest of the data.
  376. */
  377. static int i810_dma_init_compat(drm_i810_init_t * init, unsigned long arg)
  378. {
  379. /* Get v1.1 init data */
  380. if (copy_from_user(init, (drm_i810_pre12_init_t __user *) arg,
  381. sizeof(drm_i810_pre12_init_t))) {
  382. return -EFAULT;
  383. }
  384. if ((!init->overlay_physical) || (init->overlay_physical > 4096)) {
  385. /* This is a v1.2 client, just get the v1.2 init data */
  386. DRM_INFO("Using POST v1.2 init.\n");
  387. if (copy_from_user(init, (drm_i810_init_t __user *) arg,
  388. sizeof(drm_i810_init_t))) {
  389. return -EFAULT;
  390. }
  391. } else {
  392. /* This is a v1.1 client, fix the params */
  393. DRM_INFO("Using PRE v1.2 init.\n");
  394. init->pitch_bits = init->h;
  395. init->pitch = init->w;
  396. init->h = init->overlay_physical;
  397. init->w = init->overlay_offset;
  398. init->overlay_physical = 0;
  399. init->overlay_offset = 0;
  400. }
  401. return 0;
  402. }
  403. static int i810_dma_init(struct inode *inode, struct file *filp,
  404. unsigned int cmd, unsigned long arg)
  405. {
  406. struct drm_file *priv = filp->private_data;
  407. struct drm_device *dev = priv->head->dev;
  408. drm_i810_private_t *dev_priv;
  409. drm_i810_init_t init;
  410. int retcode = 0;
  411. /* Get only the init func */
  412. if (copy_from_user
  413. (&init, (void __user *)arg, sizeof(drm_i810_init_func_t)))
  414. return -EFAULT;
  415. switch (init.func) {
  416. case I810_INIT_DMA:
  417. /* This case is for backward compatibility. It
  418. * handles XFree 4.1.0 and 4.2.0, and has to
  419. * do some parameter checking as described below.
  420. * It will someday go away.
  421. */
  422. retcode = i810_dma_init_compat(&init, arg);
  423. if (retcode)
  424. return retcode;
  425. dev_priv = drm_alloc(sizeof(drm_i810_private_t),
  426. DRM_MEM_DRIVER);
  427. if (dev_priv == NULL)
  428. return -ENOMEM;
  429. retcode = i810_dma_initialize(dev, dev_priv, &init);
  430. break;
  431. default:
  432. case I810_INIT_DMA_1_4:
  433. DRM_INFO("Using v1.4 init.\n");
  434. if (copy_from_user(&init, (drm_i810_init_t __user *) arg,
  435. sizeof(drm_i810_init_t))) {
  436. return -EFAULT;
  437. }
  438. dev_priv = drm_alloc(sizeof(drm_i810_private_t),
  439. DRM_MEM_DRIVER);
  440. if (dev_priv == NULL)
  441. return -ENOMEM;
  442. retcode = i810_dma_initialize(dev, dev_priv, &init);
  443. break;
  444. case I810_CLEANUP_DMA:
  445. DRM_INFO("DMA Cleanup\n");
  446. retcode = i810_dma_cleanup(dev);
  447. break;
  448. }
  449. return retcode;
  450. }
  451. /* Most efficient way to verify state for the i810 is as it is
  452. * emitted. Non-conformant state is silently dropped.
  453. *
  454. * Use 'volatile' & local var tmp to force the emitted values to be
  455. * identical to the verified ones.
  456. */
  457. static void i810EmitContextVerified(struct drm_device * dev,
  458. volatile unsigned int *code)
  459. {
  460. drm_i810_private_t *dev_priv = dev->dev_private;
  461. int i, j = 0;
  462. unsigned int tmp;
  463. RING_LOCALS;
  464. BEGIN_LP_RING(I810_CTX_SETUP_SIZE);
  465. OUT_RING(GFX_OP_COLOR_FACTOR);
  466. OUT_RING(code[I810_CTXREG_CF1]);
  467. OUT_RING(GFX_OP_STIPPLE);
  468. OUT_RING(code[I810_CTXREG_ST1]);
  469. for (i = 4; i < I810_CTX_SETUP_SIZE; i++) {
  470. tmp = code[i];
  471. if ((tmp & (7 << 29)) == (3 << 29) &&
  472. (tmp & (0x1f << 24)) < (0x1d << 24)) {
  473. OUT_RING(tmp);
  474. j++;
  475. } else
  476. printk("constext state dropped!!!\n");
  477. }
  478. if (j & 1)
  479. OUT_RING(0);
  480. ADVANCE_LP_RING();
  481. }
  482. static void i810EmitTexVerified(struct drm_device * dev, volatile unsigned int *code)
  483. {
  484. drm_i810_private_t *dev_priv = dev->dev_private;
  485. int i, j = 0;
  486. unsigned int tmp;
  487. RING_LOCALS;
  488. BEGIN_LP_RING(I810_TEX_SETUP_SIZE);
  489. OUT_RING(GFX_OP_MAP_INFO);
  490. OUT_RING(code[I810_TEXREG_MI1]);
  491. OUT_RING(code[I810_TEXREG_MI2]);
  492. OUT_RING(code[I810_TEXREG_MI3]);
  493. for (i = 4; i < I810_TEX_SETUP_SIZE; i++) {
  494. tmp = code[i];
  495. if ((tmp & (7 << 29)) == (3 << 29) &&
  496. (tmp & (0x1f << 24)) < (0x1d << 24)) {
  497. OUT_RING(tmp);
  498. j++;
  499. } else
  500. printk("texture state dropped!!!\n");
  501. }
  502. if (j & 1)
  503. OUT_RING(0);
  504. ADVANCE_LP_RING();
  505. }
  506. /* Need to do some additional checking when setting the dest buffer.
  507. */
  508. static void i810EmitDestVerified(struct drm_device * dev,
  509. volatile unsigned int *code)
  510. {
  511. drm_i810_private_t *dev_priv = dev->dev_private;
  512. unsigned int tmp;
  513. RING_LOCALS;
  514. BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
  515. tmp = code[I810_DESTREG_DI1];
  516. if (tmp == dev_priv->front_di1 || tmp == dev_priv->back_di1) {
  517. OUT_RING(CMD_OP_DESTBUFFER_INFO);
  518. OUT_RING(tmp);
  519. } else
  520. DRM_DEBUG("bad di1 %x (allow %x or %x)\n",
  521. tmp, dev_priv->front_di1, dev_priv->back_di1);
  522. /* invarient:
  523. */
  524. OUT_RING(CMD_OP_Z_BUFFER_INFO);
  525. OUT_RING(dev_priv->zi1);
  526. OUT_RING(GFX_OP_DESTBUFFER_VARS);
  527. OUT_RING(code[I810_DESTREG_DV1]);
  528. OUT_RING(GFX_OP_DRAWRECT_INFO);
  529. OUT_RING(code[I810_DESTREG_DR1]);
  530. OUT_RING(code[I810_DESTREG_DR2]);
  531. OUT_RING(code[I810_DESTREG_DR3]);
  532. OUT_RING(code[I810_DESTREG_DR4]);
  533. OUT_RING(0);
  534. ADVANCE_LP_RING();
  535. }
  536. static void i810EmitState(struct drm_device * dev)
  537. {
  538. drm_i810_private_t *dev_priv = dev->dev_private;
  539. drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
  540. unsigned int dirty = sarea_priv->dirty;
  541. DRM_DEBUG("%s %x\n", __FUNCTION__, dirty);
  542. if (dirty & I810_UPLOAD_BUFFERS) {
  543. i810EmitDestVerified(dev, sarea_priv->BufferState);
  544. sarea_priv->dirty &= ~I810_UPLOAD_BUFFERS;
  545. }
  546. if (dirty & I810_UPLOAD_CTX) {
  547. i810EmitContextVerified(dev, sarea_priv->ContextState);
  548. sarea_priv->dirty &= ~I810_UPLOAD_CTX;
  549. }
  550. if (dirty & I810_UPLOAD_TEX0) {
  551. i810EmitTexVerified(dev, sarea_priv->TexState[0]);
  552. sarea_priv->dirty &= ~I810_UPLOAD_TEX0;
  553. }
  554. if (dirty & I810_UPLOAD_TEX1) {
  555. i810EmitTexVerified(dev, sarea_priv->TexState[1]);
  556. sarea_priv->dirty &= ~I810_UPLOAD_TEX1;
  557. }
  558. }
  559. /* need to verify
  560. */
  561. static void i810_dma_dispatch_clear(struct drm_device * dev, int flags,
  562. unsigned int clear_color,
  563. unsigned int clear_zval)
  564. {
  565. drm_i810_private_t *dev_priv = dev->dev_private;
  566. drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
  567. int nbox = sarea_priv->nbox;
  568. struct drm_clip_rect *pbox = sarea_priv->boxes;
  569. int pitch = dev_priv->pitch;
  570. int cpp = 2;
  571. int i;
  572. RING_LOCALS;
  573. if (dev_priv->current_page == 1) {
  574. unsigned int tmp = flags;
  575. flags &= ~(I810_FRONT | I810_BACK);
  576. if (tmp & I810_FRONT)
  577. flags |= I810_BACK;
  578. if (tmp & I810_BACK)
  579. flags |= I810_FRONT;
  580. }
  581. i810_kernel_lost_context(dev);
  582. if (nbox > I810_NR_SAREA_CLIPRECTS)
  583. nbox = I810_NR_SAREA_CLIPRECTS;
  584. for (i = 0; i < nbox; i++, pbox++) {
  585. unsigned int x = pbox->x1;
  586. unsigned int y = pbox->y1;
  587. unsigned int width = (pbox->x2 - x) * cpp;
  588. unsigned int height = pbox->y2 - y;
  589. unsigned int start = y * pitch + x * cpp;
  590. if (pbox->x1 > pbox->x2 ||
  591. pbox->y1 > pbox->y2 ||
  592. pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
  593. continue;
  594. if (flags & I810_FRONT) {
  595. BEGIN_LP_RING(6);
  596. OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
  597. OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
  598. OUT_RING((height << 16) | width);
  599. OUT_RING(start);
  600. OUT_RING(clear_color);
  601. OUT_RING(0);
  602. ADVANCE_LP_RING();
  603. }
  604. if (flags & I810_BACK) {
  605. BEGIN_LP_RING(6);
  606. OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
  607. OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
  608. OUT_RING((height << 16) | width);
  609. OUT_RING(dev_priv->back_offset + start);
  610. OUT_RING(clear_color);
  611. OUT_RING(0);
  612. ADVANCE_LP_RING();
  613. }
  614. if (flags & I810_DEPTH) {
  615. BEGIN_LP_RING(6);
  616. OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_COLOR_BLT | 0x3);
  617. OUT_RING(BR13_SOLID_PATTERN | (0xF0 << 16) | pitch);
  618. OUT_RING((height << 16) | width);
  619. OUT_RING(dev_priv->depth_offset + start);
  620. OUT_RING(clear_zval);
  621. OUT_RING(0);
  622. ADVANCE_LP_RING();
  623. }
  624. }
  625. }
  626. static void i810_dma_dispatch_swap(struct drm_device * dev)
  627. {
  628. drm_i810_private_t *dev_priv = dev->dev_private;
  629. drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
  630. int nbox = sarea_priv->nbox;
  631. struct drm_clip_rect *pbox = sarea_priv->boxes;
  632. int pitch = dev_priv->pitch;
  633. int cpp = 2;
  634. int i;
  635. RING_LOCALS;
  636. DRM_DEBUG("swapbuffers\n");
  637. i810_kernel_lost_context(dev);
  638. if (nbox > I810_NR_SAREA_CLIPRECTS)
  639. nbox = I810_NR_SAREA_CLIPRECTS;
  640. for (i = 0; i < nbox; i++, pbox++) {
  641. unsigned int w = pbox->x2 - pbox->x1;
  642. unsigned int h = pbox->y2 - pbox->y1;
  643. unsigned int dst = pbox->x1 * cpp + pbox->y1 * pitch;
  644. unsigned int start = dst;
  645. if (pbox->x1 > pbox->x2 ||
  646. pbox->y1 > pbox->y2 ||
  647. pbox->x2 > dev_priv->w || pbox->y2 > dev_priv->h)
  648. continue;
  649. BEGIN_LP_RING(6);
  650. OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
  651. OUT_RING(pitch | (0xCC << 16));
  652. OUT_RING((h << 16) | (w * cpp));
  653. if (dev_priv->current_page == 0)
  654. OUT_RING(dev_priv->front_offset + start);
  655. else
  656. OUT_RING(dev_priv->back_offset + start);
  657. OUT_RING(pitch);
  658. if (dev_priv->current_page == 0)
  659. OUT_RING(dev_priv->back_offset + start);
  660. else
  661. OUT_RING(dev_priv->front_offset + start);
  662. ADVANCE_LP_RING();
  663. }
  664. }
  665. static void i810_dma_dispatch_vertex(struct drm_device * dev,
  666. struct drm_buf * buf, int discard, int used)
  667. {
  668. drm_i810_private_t *dev_priv = dev->dev_private;
  669. drm_i810_buf_priv_t *buf_priv = buf->dev_private;
  670. drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
  671. struct drm_clip_rect *box = sarea_priv->boxes;
  672. int nbox = sarea_priv->nbox;
  673. unsigned long address = (unsigned long)buf->bus_address;
  674. unsigned long start = address - dev->agp->base;
  675. int i = 0;
  676. RING_LOCALS;
  677. i810_kernel_lost_context(dev);
  678. if (nbox > I810_NR_SAREA_CLIPRECTS)
  679. nbox = I810_NR_SAREA_CLIPRECTS;
  680. if (used > 4 * 1024)
  681. used = 0;
  682. if (sarea_priv->dirty)
  683. i810EmitState(dev);
  684. if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
  685. unsigned int prim = (sarea_priv->vertex_prim & PR_MASK);
  686. *(u32 *) buf_priv->kernel_virtual =
  687. ((GFX_OP_PRIMITIVE | prim | ((used / 4) - 2)));
  688. if (used & 4) {
  689. *(u32 *) ((char *) buf_priv->kernel_virtual + used) = 0;
  690. used += 4;
  691. }
  692. i810_unmap_buffer(buf);
  693. }
  694. if (used) {
  695. do {
  696. if (i < nbox) {
  697. BEGIN_LP_RING(4);
  698. OUT_RING(GFX_OP_SCISSOR | SC_UPDATE_SCISSOR |
  699. SC_ENABLE);
  700. OUT_RING(GFX_OP_SCISSOR_INFO);
  701. OUT_RING(box[i].x1 | (box[i].y1 << 16));
  702. OUT_RING((box[i].x2 -
  703. 1) | ((box[i].y2 - 1) << 16));
  704. ADVANCE_LP_RING();
  705. }
  706. BEGIN_LP_RING(4);
  707. OUT_RING(CMD_OP_BATCH_BUFFER);
  708. OUT_RING(start | BB1_PROTECTED);
  709. OUT_RING(start + used - 4);
  710. OUT_RING(0);
  711. ADVANCE_LP_RING();
  712. } while (++i < nbox);
  713. }
  714. if (discard) {
  715. dev_priv->counter++;
  716. (void)cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
  717. I810_BUF_HARDWARE);
  718. BEGIN_LP_RING(8);
  719. OUT_RING(CMD_STORE_DWORD_IDX);
  720. OUT_RING(20);
  721. OUT_RING(dev_priv->counter);
  722. OUT_RING(CMD_STORE_DWORD_IDX);
  723. OUT_RING(buf_priv->my_use_idx);
  724. OUT_RING(I810_BUF_FREE);
  725. OUT_RING(CMD_REPORT_HEAD);
  726. OUT_RING(0);
  727. ADVANCE_LP_RING();
  728. }
  729. }
  730. static void i810_dma_dispatch_flip(struct drm_device * dev)
  731. {
  732. drm_i810_private_t *dev_priv = dev->dev_private;
  733. int pitch = dev_priv->pitch;
  734. RING_LOCALS;
  735. DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
  736. __FUNCTION__,
  737. dev_priv->current_page,
  738. dev_priv->sarea_priv->pf_current_page);
  739. i810_kernel_lost_context(dev);
  740. BEGIN_LP_RING(2);
  741. OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
  742. OUT_RING(0);
  743. ADVANCE_LP_RING();
  744. BEGIN_LP_RING(I810_DEST_SETUP_SIZE + 2);
  745. /* On i815 at least ASYNC is buggy */
  746. /* pitch<<5 is from 11.2.8 p158,
  747. its the pitch / 8 then left shifted 8,
  748. so (pitch >> 3) << 8 */
  749. OUT_RING(CMD_OP_FRONTBUFFER_INFO | (pitch << 5) /*| ASYNC_FLIP */ );
  750. if (dev_priv->current_page == 0) {
  751. OUT_RING(dev_priv->back_offset);
  752. dev_priv->current_page = 1;
  753. } else {
  754. OUT_RING(dev_priv->front_offset);
  755. dev_priv->current_page = 0;
  756. }
  757. OUT_RING(0);
  758. ADVANCE_LP_RING();
  759. BEGIN_LP_RING(2);
  760. OUT_RING(CMD_OP_WAIT_FOR_EVENT | WAIT_FOR_PLANE_A_FLIP);
  761. OUT_RING(0);
  762. ADVANCE_LP_RING();
  763. /* Increment the frame counter. The client-side 3D driver must
  764. * throttle the framerate by waiting for this value before
  765. * performing the swapbuffer ioctl.
  766. */
  767. dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
  768. }
  769. static void i810_dma_quiescent(struct drm_device * dev)
  770. {
  771. drm_i810_private_t *dev_priv = dev->dev_private;
  772. RING_LOCALS;
  773. /* printk("%s\n", __FUNCTION__); */
  774. i810_kernel_lost_context(dev);
  775. BEGIN_LP_RING(4);
  776. OUT_RING(INST_PARSER_CLIENT | INST_OP_FLUSH | INST_FLUSH_MAP_CACHE);
  777. OUT_RING(CMD_REPORT_HEAD);
  778. OUT_RING(0);
  779. OUT_RING(0);
  780. ADVANCE_LP_RING();
  781. i810_wait_ring(dev, dev_priv->ring.Size - 8);
  782. }
  783. static int i810_flush_queue(struct drm_device * dev)
  784. {
  785. drm_i810_private_t *dev_priv = dev->dev_private;
  786. struct drm_device_dma *dma = dev->dma;
  787. int i, ret = 0;
  788. RING_LOCALS;
  789. /* printk("%s\n", __FUNCTION__); */
  790. i810_kernel_lost_context(dev);
  791. BEGIN_LP_RING(2);
  792. OUT_RING(CMD_REPORT_HEAD);
  793. OUT_RING(0);
  794. ADVANCE_LP_RING();
  795. i810_wait_ring(dev, dev_priv->ring.Size - 8);
  796. for (i = 0; i < dma->buf_count; i++) {
  797. struct drm_buf *buf = dma->buflist[i];
  798. drm_i810_buf_priv_t *buf_priv = buf->dev_private;
  799. int used = cmpxchg(buf_priv->in_use, I810_BUF_HARDWARE,
  800. I810_BUF_FREE);
  801. if (used == I810_BUF_HARDWARE)
  802. DRM_DEBUG("reclaimed from HARDWARE\n");
  803. if (used == I810_BUF_CLIENT)
  804. DRM_DEBUG("still on client\n");
  805. }
  806. return ret;
  807. }
  808. /* Must be called with the lock held */
  809. static void i810_reclaim_buffers(struct drm_device * dev, struct file *filp)
  810. {
  811. struct drm_device_dma *dma = dev->dma;
  812. int i;
  813. if (!dma)
  814. return;
  815. if (!dev->dev_private)
  816. return;
  817. if (!dma->buflist)
  818. return;
  819. i810_flush_queue(dev);
  820. for (i = 0; i < dma->buf_count; i++) {
  821. struct drm_buf *buf = dma->buflist[i];
  822. drm_i810_buf_priv_t *buf_priv = buf->dev_private;
  823. if (buf->filp == filp && buf_priv) {
  824. int used = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT,
  825. I810_BUF_FREE);
  826. if (used == I810_BUF_CLIENT)
  827. DRM_DEBUG("reclaimed from client\n");
  828. if (buf_priv->currently_mapped == I810_BUF_MAPPED)
  829. buf_priv->currently_mapped = I810_BUF_UNMAPPED;
  830. }
  831. }
  832. }
  833. static int i810_flush_ioctl(struct inode *inode, struct file *filp,
  834. unsigned int cmd, unsigned long arg)
  835. {
  836. struct drm_file *priv = filp->private_data;
  837. struct drm_device *dev = priv->head->dev;
  838. LOCK_TEST_WITH_RETURN(dev, filp);
  839. i810_flush_queue(dev);
  840. return 0;
  841. }
  842. static int i810_dma_vertex(struct inode *inode, struct file *filp,
  843. unsigned int cmd, unsigned long arg)
  844. {
  845. struct drm_file *priv = filp->private_data;
  846. struct drm_device *dev = priv->head->dev;
  847. struct drm_device_dma *dma = dev->dma;
  848. drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
  849. u32 *hw_status = dev_priv->hw_status_page;
  850. drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
  851. dev_priv->sarea_priv;
  852. drm_i810_vertex_t vertex;
  853. if (copy_from_user
  854. (&vertex, (drm_i810_vertex_t __user *) arg, sizeof(vertex)))
  855. return -EFAULT;
  856. LOCK_TEST_WITH_RETURN(dev, filp);
  857. DRM_DEBUG("i810 dma vertex, idx %d used %d discard %d\n",
  858. vertex.idx, vertex.used, vertex.discard);
  859. if (vertex.idx < 0 || vertex.idx > dma->buf_count)
  860. return -EINVAL;
  861. i810_dma_dispatch_vertex(dev,
  862. dma->buflist[vertex.idx],
  863. vertex.discard, vertex.used);
  864. atomic_add(vertex.used, &dev->counts[_DRM_STAT_SECONDARY]);
  865. atomic_inc(&dev->counts[_DRM_STAT_DMA]);
  866. sarea_priv->last_enqueue = dev_priv->counter - 1;
  867. sarea_priv->last_dispatch = (int)hw_status[5];
  868. return 0;
  869. }
  870. static int i810_clear_bufs(struct inode *inode, struct file *filp,
  871. unsigned int cmd, unsigned long arg)
  872. {
  873. struct drm_file *priv = filp->private_data;
  874. struct drm_device *dev = priv->head->dev;
  875. drm_i810_clear_t clear;
  876. if (copy_from_user
  877. (&clear, (drm_i810_clear_t __user *) arg, sizeof(clear)))
  878. return -EFAULT;
  879. LOCK_TEST_WITH_RETURN(dev, filp);
  880. /* GH: Someone's doing nasty things... */
  881. if (!dev->dev_private) {
  882. return -EINVAL;
  883. }
  884. i810_dma_dispatch_clear(dev, clear.flags,
  885. clear.clear_color, clear.clear_depth);
  886. return 0;
  887. }
  888. static int i810_swap_bufs(struct inode *inode, struct file *filp,
  889. unsigned int cmd, unsigned long arg)
  890. {
  891. struct drm_file *priv = filp->private_data;
  892. struct drm_device *dev = priv->head->dev;
  893. DRM_DEBUG("i810_swap_bufs\n");
  894. LOCK_TEST_WITH_RETURN(dev, filp);
  895. i810_dma_dispatch_swap(dev);
  896. return 0;
  897. }
  898. static int i810_getage(struct inode *inode, struct file *filp, unsigned int cmd,
  899. unsigned long arg)
  900. {
  901. struct drm_file *priv = filp->private_data;
  902. struct drm_device *dev = priv->head->dev;
  903. drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
  904. u32 *hw_status = dev_priv->hw_status_page;
  905. drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
  906. dev_priv->sarea_priv;
  907. sarea_priv->last_dispatch = (int)hw_status[5];
  908. return 0;
  909. }
  910. static int i810_getbuf(struct inode *inode, struct file *filp, unsigned int cmd,
  911. unsigned long arg)
  912. {
  913. struct drm_file *priv = filp->private_data;
  914. struct drm_device *dev = priv->head->dev;
  915. int retcode = 0;
  916. drm_i810_dma_t d;
  917. drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
  918. u32 *hw_status = dev_priv->hw_status_page;
  919. drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
  920. dev_priv->sarea_priv;
  921. if (copy_from_user(&d, (drm_i810_dma_t __user *) arg, sizeof(d)))
  922. return -EFAULT;
  923. LOCK_TEST_WITH_RETURN(dev, filp);
  924. d.granted = 0;
  925. retcode = i810_dma_get_buffer(dev, &d, filp);
  926. DRM_DEBUG("i810_dma: %d returning %d, granted = %d\n",
  927. current->pid, retcode, d.granted);
  928. if (copy_to_user((void __user *) arg, &d, sizeof(d)))
  929. return -EFAULT;
  930. sarea_priv->last_dispatch = (int)hw_status[5];
  931. return retcode;
  932. }
  933. static int i810_copybuf(struct inode *inode,
  934. struct file *filp, unsigned int cmd, unsigned long arg)
  935. {
  936. /* Never copy - 2.4.x doesn't need it */
  937. return 0;
  938. }
  939. static int i810_docopy(struct inode *inode, struct file *filp, unsigned int cmd,
  940. unsigned long arg)
  941. {
  942. /* Never copy - 2.4.x doesn't need it */
  943. return 0;
  944. }
  945. static void i810_dma_dispatch_mc(struct drm_device * dev, struct drm_buf * buf, int used,
  946. unsigned int last_render)
  947. {
  948. drm_i810_private_t *dev_priv = dev->dev_private;
  949. drm_i810_buf_priv_t *buf_priv = buf->dev_private;
  950. drm_i810_sarea_t *sarea_priv = dev_priv->sarea_priv;
  951. unsigned long address = (unsigned long)buf->bus_address;
  952. unsigned long start = address - dev->agp->base;
  953. int u;
  954. RING_LOCALS;
  955. i810_kernel_lost_context(dev);
  956. u = cmpxchg(buf_priv->in_use, I810_BUF_CLIENT, I810_BUF_HARDWARE);
  957. if (u != I810_BUF_CLIENT) {
  958. DRM_DEBUG("MC found buffer that isn't mine!\n");
  959. }
  960. if (used > 4 * 1024)
  961. used = 0;
  962. sarea_priv->dirty = 0x7f;
  963. DRM_DEBUG("dispatch mc addr 0x%lx, used 0x%x\n", address, used);
  964. dev_priv->counter++;
  965. DRM_DEBUG("dispatch counter : %ld\n", dev_priv->counter);
  966. DRM_DEBUG("i810_dma_dispatch_mc\n");
  967. DRM_DEBUG("start : %lx\n", start);
  968. DRM_DEBUG("used : %d\n", used);
  969. DRM_DEBUG("start + used - 4 : %ld\n", start + used - 4);
  970. if (buf_priv->currently_mapped == I810_BUF_MAPPED) {
  971. if (used & 4) {
  972. *(u32 *) ((char *) buf_priv->virtual + used) = 0;
  973. used += 4;
  974. }
  975. i810_unmap_buffer(buf);
  976. }
  977. BEGIN_LP_RING(4);
  978. OUT_RING(CMD_OP_BATCH_BUFFER);
  979. OUT_RING(start | BB1_PROTECTED);
  980. OUT_RING(start + used - 4);
  981. OUT_RING(0);
  982. ADVANCE_LP_RING();
  983. BEGIN_LP_RING(8);
  984. OUT_RING(CMD_STORE_DWORD_IDX);
  985. OUT_RING(buf_priv->my_use_idx);
  986. OUT_RING(I810_BUF_FREE);
  987. OUT_RING(0);
  988. OUT_RING(CMD_STORE_DWORD_IDX);
  989. OUT_RING(16);
  990. OUT_RING(last_render);
  991. OUT_RING(0);
  992. ADVANCE_LP_RING();
  993. }
  994. static int i810_dma_mc(struct inode *inode, struct file *filp,
  995. unsigned int cmd, unsigned long arg)
  996. {
  997. struct drm_file *priv = filp->private_data;
  998. struct drm_device *dev = priv->head->dev;
  999. struct drm_device_dma *dma = dev->dma;
  1000. drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
  1001. u32 *hw_status = dev_priv->hw_status_page;
  1002. drm_i810_sarea_t *sarea_priv = (drm_i810_sarea_t *)
  1003. dev_priv->sarea_priv;
  1004. drm_i810_mc_t mc;
  1005. if (copy_from_user(&mc, (drm_i810_mc_t __user *) arg, sizeof(mc)))
  1006. return -EFAULT;
  1007. LOCK_TEST_WITH_RETURN(dev, filp);
  1008. if (mc.idx >= dma->buf_count || mc.idx < 0)
  1009. return -EINVAL;
  1010. i810_dma_dispatch_mc(dev, dma->buflist[mc.idx], mc.used,
  1011. mc.last_render);
  1012. atomic_add(mc.used, &dev->counts[_DRM_STAT_SECONDARY]);
  1013. atomic_inc(&dev->counts[_DRM_STAT_DMA]);
  1014. sarea_priv->last_enqueue = dev_priv->counter - 1;
  1015. sarea_priv->last_dispatch = (int)hw_status[5];
  1016. return 0;
  1017. }
  1018. static int i810_rstatus(struct inode *inode, struct file *filp,
  1019. unsigned int cmd, unsigned long arg)
  1020. {
  1021. struct drm_file *priv = filp->private_data;
  1022. struct drm_device *dev = priv->head->dev;
  1023. drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
  1024. return (int)(((u32 *) (dev_priv->hw_status_page))[4]);
  1025. }
  1026. static int i810_ov0_info(struct inode *inode, struct file *filp,
  1027. unsigned int cmd, unsigned long arg)
  1028. {
  1029. struct drm_file *priv = filp->private_data;
  1030. struct drm_device *dev = priv->head->dev;
  1031. drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
  1032. drm_i810_overlay_t data;
  1033. data.offset = dev_priv->overlay_offset;
  1034. data.physical = dev_priv->overlay_physical;
  1035. if (copy_to_user
  1036. ((drm_i810_overlay_t __user *) arg, &data, sizeof(data)))
  1037. return -EFAULT;
  1038. return 0;
  1039. }
  1040. static int i810_fstatus(struct inode *inode, struct file *filp,
  1041. unsigned int cmd, unsigned long arg)
  1042. {
  1043. struct drm_file *priv = filp->private_data;
  1044. struct drm_device *dev = priv->head->dev;
  1045. drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
  1046. LOCK_TEST_WITH_RETURN(dev, filp);
  1047. return I810_READ(0x30008);
  1048. }
  1049. static int i810_ov0_flip(struct inode *inode, struct file *filp,
  1050. unsigned int cmd, unsigned long arg)
  1051. {
  1052. struct drm_file *priv = filp->private_data;
  1053. struct drm_device *dev = priv->head->dev;
  1054. drm_i810_private_t *dev_priv = (drm_i810_private_t *) dev->dev_private;
  1055. LOCK_TEST_WITH_RETURN(dev, filp);
  1056. //Tell the overlay to update
  1057. I810_WRITE(0x30000, dev_priv->overlay_physical | 0x80000000);
  1058. return 0;
  1059. }
  1060. /* Not sure why this isn't set all the time:
  1061. */
  1062. static void i810_do_init_pageflip(struct drm_device * dev)
  1063. {
  1064. drm_i810_private_t *dev_priv = dev->dev_private;
  1065. DRM_DEBUG("%s\n", __FUNCTION__);
  1066. dev_priv->page_flipping = 1;
  1067. dev_priv->current_page = 0;
  1068. dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
  1069. }
  1070. static int i810_do_cleanup_pageflip(struct drm_device * dev)
  1071. {
  1072. drm_i810_private_t *dev_priv = dev->dev_private;
  1073. DRM_DEBUG("%s\n", __FUNCTION__);
  1074. if (dev_priv->current_page != 0)
  1075. i810_dma_dispatch_flip(dev);
  1076. dev_priv->page_flipping = 0;
  1077. return 0;
  1078. }
  1079. static int i810_flip_bufs(struct inode *inode, struct file *filp,
  1080. unsigned int cmd, unsigned long arg)
  1081. {
  1082. struct drm_file *priv = filp->private_data;
  1083. struct drm_device *dev = priv->head->dev;
  1084. drm_i810_private_t *dev_priv = dev->dev_private;
  1085. DRM_DEBUG("%s\n", __FUNCTION__);
  1086. LOCK_TEST_WITH_RETURN(dev, filp);
  1087. if (!dev_priv->page_flipping)
  1088. i810_do_init_pageflip(dev);
  1089. i810_dma_dispatch_flip(dev);
  1090. return 0;
  1091. }
  1092. int i810_driver_load(struct drm_device *dev, unsigned long flags)
  1093. {
  1094. /* i810 has 4 more counters */
  1095. dev->counters += 4;
  1096. dev->types[6] = _DRM_STAT_IRQ;
  1097. dev->types[7] = _DRM_STAT_PRIMARY;
  1098. dev->types[8] = _DRM_STAT_SECONDARY;
  1099. dev->types[9] = _DRM_STAT_DMA;
  1100. return 0;
  1101. }
  1102. void i810_driver_lastclose(struct drm_device * dev)
  1103. {
  1104. i810_dma_cleanup(dev);
  1105. }
  1106. void i810_driver_preclose(struct drm_device * dev, DRMFILE filp)
  1107. {
  1108. if (dev->dev_private) {
  1109. drm_i810_private_t *dev_priv = dev->dev_private;
  1110. if (dev_priv->page_flipping) {
  1111. i810_do_cleanup_pageflip(dev);
  1112. }
  1113. }
  1114. }
  1115. void i810_driver_reclaim_buffers_locked(struct drm_device * dev, struct file *filp)
  1116. {
  1117. i810_reclaim_buffers(dev, filp);
  1118. }
  1119. int i810_driver_dma_quiescent(struct drm_device * dev)
  1120. {
  1121. i810_dma_quiescent(dev);
  1122. return 0;
  1123. }
  1124. drm_ioctl_desc_t i810_ioctls[] = {
  1125. [DRM_IOCTL_NR(DRM_I810_INIT)] = {i810_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
  1126. [DRM_IOCTL_NR(DRM_I810_VERTEX)] = {i810_dma_vertex, DRM_AUTH},
  1127. [DRM_IOCTL_NR(DRM_I810_CLEAR)] = {i810_clear_bufs, DRM_AUTH},
  1128. [DRM_IOCTL_NR(DRM_I810_FLUSH)] = {i810_flush_ioctl, DRM_AUTH},
  1129. [DRM_IOCTL_NR(DRM_I810_GETAGE)] = {i810_getage, DRM_AUTH},
  1130. [DRM_IOCTL_NR(DRM_I810_GETBUF)] = {i810_getbuf, DRM_AUTH},
  1131. [DRM_IOCTL_NR(DRM_I810_SWAP)] = {i810_swap_bufs, DRM_AUTH},
  1132. [DRM_IOCTL_NR(DRM_I810_COPY)] = {i810_copybuf, DRM_AUTH},
  1133. [DRM_IOCTL_NR(DRM_I810_DOCOPY)] = {i810_docopy, DRM_AUTH},
  1134. [DRM_IOCTL_NR(DRM_I810_OV0INFO)] = {i810_ov0_info, DRM_AUTH},
  1135. [DRM_IOCTL_NR(DRM_I810_FSTATUS)] = {i810_fstatus, DRM_AUTH},
  1136. [DRM_IOCTL_NR(DRM_I810_OV0FLIP)] = {i810_ov0_flip, DRM_AUTH},
  1137. [DRM_IOCTL_NR(DRM_I810_MC)] = {i810_dma_mc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
  1138. [DRM_IOCTL_NR(DRM_I810_RSTATUS)] = {i810_rstatus, DRM_AUTH},
  1139. [DRM_IOCTL_NR(DRM_I810_FLIP)] = {i810_flip_bufs, DRM_AUTH}
  1140. };
  1141. int i810_max_ioctl = DRM_ARRAY_SIZE(i810_ioctls);
  1142. /**
  1143. * Determine if the device really is AGP or not.
  1144. *
  1145. * All Intel graphics chipsets are treated as AGP, even if they are really
  1146. * PCI-e.
  1147. *
  1148. * \param dev The device to be tested.
  1149. *
  1150. * \returns
  1151. * A value of 1 is always retured to indictate every i810 is AGP.
  1152. */
  1153. int i810_driver_device_is_agp(struct drm_device * dev)
  1154. {
  1155. return 1;
  1156. }