savage_bci.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /* savage_bci.c -- BCI support for Savage
  2. *
  3. * Copyright 2004 Felix Kuehling
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial portions
  15. * of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NON-INFRINGEMENT. IN NO EVENT SHALL FELIX KUEHLING BE LIABLE FOR
  21. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  22. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <drm/drmP.h>
  26. #include <drm/savage_drm.h>
  27. #include "savage_drv.h"
  28. /* Need a long timeout for shadow status updates can take a while
  29. * and so can waiting for events when the queue is full. */
  30. #define SAVAGE_DEFAULT_USEC_TIMEOUT 1000000 /* 1s */
  31. #define SAVAGE_EVENT_USEC_TIMEOUT 5000000 /* 5s */
  32. #define SAVAGE_FREELIST_DEBUG 0
  33. static int savage_do_cleanup_bci(struct drm_device *dev);
  34. static int
  35. savage_bci_wait_fifo_shadow(drm_savage_private_t * dev_priv, unsigned int n)
  36. {
  37. uint32_t mask = dev_priv->status_used_mask;
  38. uint32_t threshold = dev_priv->bci_threshold_hi;
  39. uint32_t status;
  40. int i;
  41. #if SAVAGE_BCI_DEBUG
  42. if (n > dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - threshold)
  43. DRM_ERROR("Trying to emit %d words "
  44. "(more than guaranteed space in COB)\n", n);
  45. #endif
  46. for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
  47. mb();
  48. status = dev_priv->status_ptr[0];
  49. if ((status & mask) < threshold)
  50. return 0;
  51. DRM_UDELAY(1);
  52. }
  53. #if SAVAGE_BCI_DEBUG
  54. DRM_ERROR("failed!\n");
  55. DRM_INFO(" status=0x%08x, threshold=0x%08x\n", status, threshold);
  56. #endif
  57. return -EBUSY;
  58. }
  59. static int
  60. savage_bci_wait_fifo_s3d(drm_savage_private_t * dev_priv, unsigned int n)
  61. {
  62. uint32_t maxUsed = dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - n;
  63. uint32_t status;
  64. int i;
  65. for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
  66. status = SAVAGE_READ(SAVAGE_STATUS_WORD0);
  67. if ((status & SAVAGE_FIFO_USED_MASK_S3D) <= maxUsed)
  68. return 0;
  69. DRM_UDELAY(1);
  70. }
  71. #if SAVAGE_BCI_DEBUG
  72. DRM_ERROR("failed!\n");
  73. DRM_INFO(" status=0x%08x\n", status);
  74. #endif
  75. return -EBUSY;
  76. }
  77. static int
  78. savage_bci_wait_fifo_s4(drm_savage_private_t * dev_priv, unsigned int n)
  79. {
  80. uint32_t maxUsed = dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - n;
  81. uint32_t status;
  82. int i;
  83. for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
  84. status = SAVAGE_READ(SAVAGE_ALT_STATUS_WORD0);
  85. if ((status & SAVAGE_FIFO_USED_MASK_S4) <= maxUsed)
  86. return 0;
  87. DRM_UDELAY(1);
  88. }
  89. #if SAVAGE_BCI_DEBUG
  90. DRM_ERROR("failed!\n");
  91. DRM_INFO(" status=0x%08x\n", status);
  92. #endif
  93. return -EBUSY;
  94. }
  95. /*
  96. * Waiting for events.
  97. *
  98. * The BIOSresets the event tag to 0 on mode changes. Therefore we
  99. * never emit 0 to the event tag. If we find a 0 event tag we know the
  100. * BIOS stomped on it and return success assuming that the BIOS waited
  101. * for engine idle.
  102. *
  103. * Note: if the Xserver uses the event tag it has to follow the same
  104. * rule. Otherwise there may be glitches every 2^16 events.
  105. */
  106. static int
  107. savage_bci_wait_event_shadow(drm_savage_private_t * dev_priv, uint16_t e)
  108. {
  109. uint32_t status;
  110. int i;
  111. for (i = 0; i < SAVAGE_EVENT_USEC_TIMEOUT; i++) {
  112. mb();
  113. status = dev_priv->status_ptr[1];
  114. if ((((status & 0xffff) - e) & 0xffff) <= 0x7fff ||
  115. (status & 0xffff) == 0)
  116. return 0;
  117. DRM_UDELAY(1);
  118. }
  119. #if SAVAGE_BCI_DEBUG
  120. DRM_ERROR("failed!\n");
  121. DRM_INFO(" status=0x%08x, e=0x%04x\n", status, e);
  122. #endif
  123. return -EBUSY;
  124. }
  125. static int
  126. savage_bci_wait_event_reg(drm_savage_private_t * dev_priv, uint16_t e)
  127. {
  128. uint32_t status;
  129. int i;
  130. for (i = 0; i < SAVAGE_EVENT_USEC_TIMEOUT; i++) {
  131. status = SAVAGE_READ(SAVAGE_STATUS_WORD1);
  132. if ((((status & 0xffff) - e) & 0xffff) <= 0x7fff ||
  133. (status & 0xffff) == 0)
  134. return 0;
  135. DRM_UDELAY(1);
  136. }
  137. #if SAVAGE_BCI_DEBUG
  138. DRM_ERROR("failed!\n");
  139. DRM_INFO(" status=0x%08x, e=0x%04x\n", status, e);
  140. #endif
  141. return -EBUSY;
  142. }
  143. uint16_t savage_bci_emit_event(drm_savage_private_t * dev_priv,
  144. unsigned int flags)
  145. {
  146. uint16_t count;
  147. BCI_LOCALS;
  148. if (dev_priv->status_ptr) {
  149. /* coordinate with Xserver */
  150. count = dev_priv->status_ptr[1023];
  151. if (count < dev_priv->event_counter)
  152. dev_priv->event_wrap++;
  153. } else {
  154. count = dev_priv->event_counter;
  155. }
  156. count = (count + 1) & 0xffff;
  157. if (count == 0) {
  158. count++; /* See the comment above savage_wait_event_*. */
  159. dev_priv->event_wrap++;
  160. }
  161. dev_priv->event_counter = count;
  162. if (dev_priv->status_ptr)
  163. dev_priv->status_ptr[1023] = (uint32_t) count;
  164. if ((flags & (SAVAGE_WAIT_2D | SAVAGE_WAIT_3D))) {
  165. unsigned int wait_cmd = BCI_CMD_WAIT;
  166. if ((flags & SAVAGE_WAIT_2D))
  167. wait_cmd |= BCI_CMD_WAIT_2D;
  168. if ((flags & SAVAGE_WAIT_3D))
  169. wait_cmd |= BCI_CMD_WAIT_3D;
  170. BEGIN_BCI(2);
  171. BCI_WRITE(wait_cmd);
  172. } else {
  173. BEGIN_BCI(1);
  174. }
  175. BCI_WRITE(BCI_CMD_UPDATE_EVENT_TAG | (uint32_t) count);
  176. return count;
  177. }
  178. /*
  179. * Freelist management
  180. */
  181. static int savage_freelist_init(struct drm_device * dev)
  182. {
  183. drm_savage_private_t *dev_priv = dev->dev_private;
  184. struct drm_device_dma *dma = dev->dma;
  185. struct drm_buf *buf;
  186. drm_savage_buf_priv_t *entry;
  187. int i;
  188. DRM_DEBUG("count=%d\n", dma->buf_count);
  189. dev_priv->head.next = &dev_priv->tail;
  190. dev_priv->head.prev = NULL;
  191. dev_priv->head.buf = NULL;
  192. dev_priv->tail.next = NULL;
  193. dev_priv->tail.prev = &dev_priv->head;
  194. dev_priv->tail.buf = NULL;
  195. for (i = 0; i < dma->buf_count; i++) {
  196. buf = dma->buflist[i];
  197. entry = buf->dev_private;
  198. SET_AGE(&entry->age, 0, 0);
  199. entry->buf = buf;
  200. entry->next = dev_priv->head.next;
  201. entry->prev = &dev_priv->head;
  202. dev_priv->head.next->prev = entry;
  203. dev_priv->head.next = entry;
  204. }
  205. return 0;
  206. }
  207. static struct drm_buf *savage_freelist_get(struct drm_device * dev)
  208. {
  209. drm_savage_private_t *dev_priv = dev->dev_private;
  210. drm_savage_buf_priv_t *tail = dev_priv->tail.prev;
  211. uint16_t event;
  212. unsigned int wrap;
  213. DRM_DEBUG("\n");
  214. UPDATE_EVENT_COUNTER();
  215. if (dev_priv->status_ptr)
  216. event = dev_priv->status_ptr[1] & 0xffff;
  217. else
  218. event = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
  219. wrap = dev_priv->event_wrap;
  220. if (event > dev_priv->event_counter)
  221. wrap--; /* hardware hasn't passed the last wrap yet */
  222. DRM_DEBUG(" tail=0x%04x %d\n", tail->age.event, tail->age.wrap);
  223. DRM_DEBUG(" head=0x%04x %d\n", event, wrap);
  224. if (tail->buf && (TEST_AGE(&tail->age, event, wrap) || event == 0)) {
  225. drm_savage_buf_priv_t *next = tail->next;
  226. drm_savage_buf_priv_t *prev = tail->prev;
  227. prev->next = next;
  228. next->prev = prev;
  229. tail->next = tail->prev = NULL;
  230. return tail->buf;
  231. }
  232. DRM_DEBUG("returning NULL, tail->buf=%p!\n", tail->buf);
  233. return NULL;
  234. }
  235. void savage_freelist_put(struct drm_device * dev, struct drm_buf * buf)
  236. {
  237. drm_savage_private_t *dev_priv = dev->dev_private;
  238. drm_savage_buf_priv_t *entry = buf->dev_private, *prev, *next;
  239. DRM_DEBUG("age=0x%04x wrap=%d\n", entry->age.event, entry->age.wrap);
  240. if (entry->next != NULL || entry->prev != NULL) {
  241. DRM_ERROR("entry already on freelist.\n");
  242. return;
  243. }
  244. prev = &dev_priv->head;
  245. next = prev->next;
  246. prev->next = entry;
  247. next->prev = entry;
  248. entry->prev = prev;
  249. entry->next = next;
  250. }
  251. /*
  252. * Command DMA
  253. */
  254. static int savage_dma_init(drm_savage_private_t * dev_priv)
  255. {
  256. unsigned int i;
  257. dev_priv->nr_dma_pages = dev_priv->cmd_dma->size /
  258. (SAVAGE_DMA_PAGE_SIZE * 4);
  259. dev_priv->dma_pages = kmalloc(sizeof(drm_savage_dma_page_t) *
  260. dev_priv->nr_dma_pages, GFP_KERNEL);
  261. if (dev_priv->dma_pages == NULL)
  262. return -ENOMEM;
  263. for (i = 0; i < dev_priv->nr_dma_pages; ++i) {
  264. SET_AGE(&dev_priv->dma_pages[i].age, 0, 0);
  265. dev_priv->dma_pages[i].used = 0;
  266. dev_priv->dma_pages[i].flushed = 0;
  267. }
  268. SET_AGE(&dev_priv->last_dma_age, 0, 0);
  269. dev_priv->first_dma_page = 0;
  270. dev_priv->current_dma_page = 0;
  271. return 0;
  272. }
  273. void savage_dma_reset(drm_savage_private_t * dev_priv)
  274. {
  275. uint16_t event;
  276. unsigned int wrap, i;
  277. event = savage_bci_emit_event(dev_priv, 0);
  278. wrap = dev_priv->event_wrap;
  279. for (i = 0; i < dev_priv->nr_dma_pages; ++i) {
  280. SET_AGE(&dev_priv->dma_pages[i].age, event, wrap);
  281. dev_priv->dma_pages[i].used = 0;
  282. dev_priv->dma_pages[i].flushed = 0;
  283. }
  284. SET_AGE(&dev_priv->last_dma_age, event, wrap);
  285. dev_priv->first_dma_page = dev_priv->current_dma_page = 0;
  286. }
  287. void savage_dma_wait(drm_savage_private_t * dev_priv, unsigned int page)
  288. {
  289. uint16_t event;
  290. unsigned int wrap;
  291. /* Faked DMA buffer pages don't age. */
  292. if (dev_priv->cmd_dma == &dev_priv->fake_dma)
  293. return;
  294. UPDATE_EVENT_COUNTER();
  295. if (dev_priv->status_ptr)
  296. event = dev_priv->status_ptr[1] & 0xffff;
  297. else
  298. event = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
  299. wrap = dev_priv->event_wrap;
  300. if (event > dev_priv->event_counter)
  301. wrap--; /* hardware hasn't passed the last wrap yet */
  302. if (dev_priv->dma_pages[page].age.wrap > wrap ||
  303. (dev_priv->dma_pages[page].age.wrap == wrap &&
  304. dev_priv->dma_pages[page].age.event > event)) {
  305. if (dev_priv->wait_evnt(dev_priv,
  306. dev_priv->dma_pages[page].age.event)
  307. < 0)
  308. DRM_ERROR("wait_evnt failed!\n");
  309. }
  310. }
  311. uint32_t *savage_dma_alloc(drm_savage_private_t * dev_priv, unsigned int n)
  312. {
  313. unsigned int cur = dev_priv->current_dma_page;
  314. unsigned int rest = SAVAGE_DMA_PAGE_SIZE -
  315. dev_priv->dma_pages[cur].used;
  316. unsigned int nr_pages = (n - rest + SAVAGE_DMA_PAGE_SIZE - 1) /
  317. SAVAGE_DMA_PAGE_SIZE;
  318. uint32_t *dma_ptr;
  319. unsigned int i;
  320. DRM_DEBUG("cur=%u, cur->used=%u, n=%u, rest=%u, nr_pages=%u\n",
  321. cur, dev_priv->dma_pages[cur].used, n, rest, nr_pages);
  322. if (cur + nr_pages < dev_priv->nr_dma_pages) {
  323. dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
  324. cur * SAVAGE_DMA_PAGE_SIZE + dev_priv->dma_pages[cur].used;
  325. if (n < rest)
  326. rest = n;
  327. dev_priv->dma_pages[cur].used += rest;
  328. n -= rest;
  329. cur++;
  330. } else {
  331. dev_priv->dma_flush(dev_priv);
  332. nr_pages =
  333. (n + SAVAGE_DMA_PAGE_SIZE - 1) / SAVAGE_DMA_PAGE_SIZE;
  334. for (i = cur; i < dev_priv->nr_dma_pages; ++i) {
  335. dev_priv->dma_pages[i].age = dev_priv->last_dma_age;
  336. dev_priv->dma_pages[i].used = 0;
  337. dev_priv->dma_pages[i].flushed = 0;
  338. }
  339. dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle;
  340. dev_priv->first_dma_page = cur = 0;
  341. }
  342. for (i = cur; nr_pages > 0; ++i, --nr_pages) {
  343. #if SAVAGE_DMA_DEBUG
  344. if (dev_priv->dma_pages[i].used) {
  345. DRM_ERROR("unflushed page %u: used=%u\n",
  346. i, dev_priv->dma_pages[i].used);
  347. }
  348. #endif
  349. if (n > SAVAGE_DMA_PAGE_SIZE)
  350. dev_priv->dma_pages[i].used = SAVAGE_DMA_PAGE_SIZE;
  351. else
  352. dev_priv->dma_pages[i].used = n;
  353. n -= SAVAGE_DMA_PAGE_SIZE;
  354. }
  355. dev_priv->current_dma_page = --i;
  356. DRM_DEBUG("cur=%u, cur->used=%u, n=%u\n",
  357. i, dev_priv->dma_pages[i].used, n);
  358. savage_dma_wait(dev_priv, dev_priv->current_dma_page);
  359. return dma_ptr;
  360. }
  361. static void savage_dma_flush(drm_savage_private_t * dev_priv)
  362. {
  363. unsigned int first = dev_priv->first_dma_page;
  364. unsigned int cur = dev_priv->current_dma_page;
  365. uint16_t event;
  366. unsigned int wrap, pad, align, len, i;
  367. unsigned long phys_addr;
  368. BCI_LOCALS;
  369. if (first == cur &&
  370. dev_priv->dma_pages[cur].used == dev_priv->dma_pages[cur].flushed)
  371. return;
  372. /* pad length to multiples of 2 entries
  373. * align start of next DMA block to multiles of 8 entries */
  374. pad = -dev_priv->dma_pages[cur].used & 1;
  375. align = -(dev_priv->dma_pages[cur].used + pad) & 7;
  376. DRM_DEBUG("first=%u, cur=%u, first->flushed=%u, cur->used=%u, "
  377. "pad=%u, align=%u\n",
  378. first, cur, dev_priv->dma_pages[first].flushed,
  379. dev_priv->dma_pages[cur].used, pad, align);
  380. /* pad with noops */
  381. if (pad) {
  382. uint32_t *dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
  383. cur * SAVAGE_DMA_PAGE_SIZE + dev_priv->dma_pages[cur].used;
  384. dev_priv->dma_pages[cur].used += pad;
  385. while (pad != 0) {
  386. *dma_ptr++ = BCI_CMD_WAIT;
  387. pad--;
  388. }
  389. }
  390. mb();
  391. /* do flush ... */
  392. phys_addr = dev_priv->cmd_dma->offset +
  393. (first * SAVAGE_DMA_PAGE_SIZE +
  394. dev_priv->dma_pages[first].flushed) * 4;
  395. len = (cur - first) * SAVAGE_DMA_PAGE_SIZE +
  396. dev_priv->dma_pages[cur].used - dev_priv->dma_pages[first].flushed;
  397. DRM_DEBUG("phys_addr=%lx, len=%u\n",
  398. phys_addr | dev_priv->dma_type, len);
  399. BEGIN_BCI(3);
  400. BCI_SET_REGISTERS(SAVAGE_DMABUFADDR, 1);
  401. BCI_WRITE(phys_addr | dev_priv->dma_type);
  402. BCI_DMA(len);
  403. /* fix alignment of the start of the next block */
  404. dev_priv->dma_pages[cur].used += align;
  405. /* age DMA pages */
  406. event = savage_bci_emit_event(dev_priv, 0);
  407. wrap = dev_priv->event_wrap;
  408. for (i = first; i < cur; ++i) {
  409. SET_AGE(&dev_priv->dma_pages[i].age, event, wrap);
  410. dev_priv->dma_pages[i].used = 0;
  411. dev_priv->dma_pages[i].flushed = 0;
  412. }
  413. /* age the current page only when it's full */
  414. if (dev_priv->dma_pages[cur].used == SAVAGE_DMA_PAGE_SIZE) {
  415. SET_AGE(&dev_priv->dma_pages[cur].age, event, wrap);
  416. dev_priv->dma_pages[cur].used = 0;
  417. dev_priv->dma_pages[cur].flushed = 0;
  418. /* advance to next page */
  419. cur++;
  420. if (cur == dev_priv->nr_dma_pages)
  421. cur = 0;
  422. dev_priv->first_dma_page = dev_priv->current_dma_page = cur;
  423. } else {
  424. dev_priv->first_dma_page = cur;
  425. dev_priv->dma_pages[cur].flushed = dev_priv->dma_pages[i].used;
  426. }
  427. SET_AGE(&dev_priv->last_dma_age, event, wrap);
  428. DRM_DEBUG("first=cur=%u, cur->used=%u, cur->flushed=%u\n", cur,
  429. dev_priv->dma_pages[cur].used,
  430. dev_priv->dma_pages[cur].flushed);
  431. }
  432. static void savage_fake_dma_flush(drm_savage_private_t * dev_priv)
  433. {
  434. unsigned int i, j;
  435. BCI_LOCALS;
  436. if (dev_priv->first_dma_page == dev_priv->current_dma_page &&
  437. dev_priv->dma_pages[dev_priv->current_dma_page].used == 0)
  438. return;
  439. DRM_DEBUG("first=%u, cur=%u, cur->used=%u\n",
  440. dev_priv->first_dma_page, dev_priv->current_dma_page,
  441. dev_priv->dma_pages[dev_priv->current_dma_page].used);
  442. for (i = dev_priv->first_dma_page;
  443. i <= dev_priv->current_dma_page && dev_priv->dma_pages[i].used;
  444. ++i) {
  445. uint32_t *dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
  446. i * SAVAGE_DMA_PAGE_SIZE;
  447. #if SAVAGE_DMA_DEBUG
  448. /* Sanity check: all pages except the last one must be full. */
  449. if (i < dev_priv->current_dma_page &&
  450. dev_priv->dma_pages[i].used != SAVAGE_DMA_PAGE_SIZE) {
  451. DRM_ERROR("partial DMA page %u: used=%u",
  452. i, dev_priv->dma_pages[i].used);
  453. }
  454. #endif
  455. BEGIN_BCI(dev_priv->dma_pages[i].used);
  456. for (j = 0; j < dev_priv->dma_pages[i].used; ++j) {
  457. BCI_WRITE(dma_ptr[j]);
  458. }
  459. dev_priv->dma_pages[i].used = 0;
  460. }
  461. /* reset to first page */
  462. dev_priv->first_dma_page = dev_priv->current_dma_page = 0;
  463. }
  464. int savage_driver_load(struct drm_device *dev, unsigned long chipset)
  465. {
  466. drm_savage_private_t *dev_priv;
  467. dev_priv = kzalloc(sizeof(drm_savage_private_t), GFP_KERNEL);
  468. if (dev_priv == NULL)
  469. return -ENOMEM;
  470. dev->dev_private = (void *)dev_priv;
  471. dev_priv->chipset = (enum savage_family)chipset;
  472. pci_set_master(dev->pdev);
  473. return 0;
  474. }
  475. /*
  476. * Initialize mappings. On Savage4 and SavageIX the alignment
  477. * and size of the aperture is not suitable for automatic MTRR setup
  478. * in drm_legacy_addmap. Therefore we add them manually before the maps are
  479. * initialized, and tear them down on last close.
  480. */
  481. int savage_driver_firstopen(struct drm_device *dev)
  482. {
  483. drm_savage_private_t *dev_priv = dev->dev_private;
  484. unsigned long mmio_base, fb_base, fb_size, aperture_base;
  485. /* fb_rsrc and aper_rsrc aren't really used currently, but still exist
  486. * in case we decide we need information on the BAR for BSD in the
  487. * future.
  488. */
  489. unsigned int fb_rsrc, aper_rsrc;
  490. int ret = 0;
  491. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  492. fb_rsrc = 0;
  493. fb_base = pci_resource_start(dev->pdev, 0);
  494. fb_size = SAVAGE_FB_SIZE_S3;
  495. mmio_base = fb_base + SAVAGE_FB_SIZE_S3;
  496. aper_rsrc = 0;
  497. aperture_base = fb_base + SAVAGE_APERTURE_OFFSET;
  498. /* this should always be true */
  499. if (pci_resource_len(dev->pdev, 0) == 0x08000000) {
  500. /* Don't make MMIO write-cobining! We need 3
  501. * MTRRs. */
  502. dev_priv->mtrr_handles[0] =
  503. arch_phys_wc_add(fb_base, 0x01000000);
  504. dev_priv->mtrr_handles[1] =
  505. arch_phys_wc_add(fb_base + 0x02000000,
  506. 0x02000000);
  507. dev_priv->mtrr_handles[2] =
  508. arch_phys_wc_add(fb_base + 0x04000000,
  509. 0x04000000);
  510. } else {
  511. DRM_ERROR("strange pci_resource_len %08llx\n",
  512. (unsigned long long)
  513. pci_resource_len(dev->pdev, 0));
  514. }
  515. } else if (dev_priv->chipset != S3_SUPERSAVAGE &&
  516. dev_priv->chipset != S3_SAVAGE2000) {
  517. mmio_base = pci_resource_start(dev->pdev, 0);
  518. fb_rsrc = 1;
  519. fb_base = pci_resource_start(dev->pdev, 1);
  520. fb_size = SAVAGE_FB_SIZE_S4;
  521. aper_rsrc = 1;
  522. aperture_base = fb_base + SAVAGE_APERTURE_OFFSET;
  523. /* this should always be true */
  524. if (pci_resource_len(dev->pdev, 1) == 0x08000000) {
  525. /* Can use one MTRR to cover both fb and
  526. * aperture. */
  527. dev_priv->mtrr_handles[0] =
  528. arch_phys_wc_add(fb_base,
  529. 0x08000000);
  530. } else {
  531. DRM_ERROR("strange pci_resource_len %08llx\n",
  532. (unsigned long long)
  533. pci_resource_len(dev->pdev, 1));
  534. }
  535. } else {
  536. mmio_base = pci_resource_start(dev->pdev, 0);
  537. fb_rsrc = 1;
  538. fb_base = pci_resource_start(dev->pdev, 1);
  539. fb_size = pci_resource_len(dev->pdev, 1);
  540. aper_rsrc = 2;
  541. aperture_base = pci_resource_start(dev->pdev, 2);
  542. /* Automatic MTRR setup will do the right thing. */
  543. }
  544. ret = drm_legacy_addmap(dev, mmio_base, SAVAGE_MMIO_SIZE,
  545. _DRM_REGISTERS, _DRM_READ_ONLY,
  546. &dev_priv->mmio);
  547. if (ret)
  548. return ret;
  549. ret = drm_legacy_addmap(dev, fb_base, fb_size, _DRM_FRAME_BUFFER,
  550. _DRM_WRITE_COMBINING, &dev_priv->fb);
  551. if (ret)
  552. return ret;
  553. ret = drm_legacy_addmap(dev, aperture_base, SAVAGE_APERTURE_SIZE,
  554. _DRM_FRAME_BUFFER, _DRM_WRITE_COMBINING,
  555. &dev_priv->aperture);
  556. return ret;
  557. }
  558. /*
  559. * Delete MTRRs and free device-private data.
  560. */
  561. void savage_driver_lastclose(struct drm_device *dev)
  562. {
  563. drm_savage_private_t *dev_priv = dev->dev_private;
  564. int i;
  565. for (i = 0; i < 3; ++i) {
  566. arch_phys_wc_del(dev_priv->mtrr_handles[i]);
  567. dev_priv->mtrr_handles[i] = 0;
  568. }
  569. }
  570. void savage_driver_unload(struct drm_device *dev)
  571. {
  572. drm_savage_private_t *dev_priv = dev->dev_private;
  573. kfree(dev_priv);
  574. }
  575. static int savage_do_init_bci(struct drm_device * dev, drm_savage_init_t * init)
  576. {
  577. drm_savage_private_t *dev_priv = dev->dev_private;
  578. if (init->fb_bpp != 16 && init->fb_bpp != 32) {
  579. DRM_ERROR("invalid frame buffer bpp %d!\n", init->fb_bpp);
  580. return -EINVAL;
  581. }
  582. if (init->depth_bpp != 16 && init->depth_bpp != 32) {
  583. DRM_ERROR("invalid depth buffer bpp %d!\n", init->fb_bpp);
  584. return -EINVAL;
  585. }
  586. if (init->dma_type != SAVAGE_DMA_AGP &&
  587. init->dma_type != SAVAGE_DMA_PCI) {
  588. DRM_ERROR("invalid dma memory type %d!\n", init->dma_type);
  589. return -EINVAL;
  590. }
  591. dev_priv->cob_size = init->cob_size;
  592. dev_priv->bci_threshold_lo = init->bci_threshold_lo;
  593. dev_priv->bci_threshold_hi = init->bci_threshold_hi;
  594. dev_priv->dma_type = init->dma_type;
  595. dev_priv->fb_bpp = init->fb_bpp;
  596. dev_priv->front_offset = init->front_offset;
  597. dev_priv->front_pitch = init->front_pitch;
  598. dev_priv->back_offset = init->back_offset;
  599. dev_priv->back_pitch = init->back_pitch;
  600. dev_priv->depth_bpp = init->depth_bpp;
  601. dev_priv->depth_offset = init->depth_offset;
  602. dev_priv->depth_pitch = init->depth_pitch;
  603. dev_priv->texture_offset = init->texture_offset;
  604. dev_priv->texture_size = init->texture_size;
  605. dev_priv->sarea = drm_legacy_getsarea(dev);
  606. if (!dev_priv->sarea) {
  607. DRM_ERROR("could not find sarea!\n");
  608. savage_do_cleanup_bci(dev);
  609. return -EINVAL;
  610. }
  611. if (init->status_offset != 0) {
  612. dev_priv->status = drm_legacy_findmap(dev, init->status_offset);
  613. if (!dev_priv->status) {
  614. DRM_ERROR("could not find shadow status region!\n");
  615. savage_do_cleanup_bci(dev);
  616. return -EINVAL;
  617. }
  618. } else {
  619. dev_priv->status = NULL;
  620. }
  621. if (dev_priv->dma_type == SAVAGE_DMA_AGP && init->buffers_offset) {
  622. dev->agp_buffer_token = init->buffers_offset;
  623. dev->agp_buffer_map = drm_legacy_findmap(dev,
  624. init->buffers_offset);
  625. if (!dev->agp_buffer_map) {
  626. DRM_ERROR("could not find DMA buffer region!\n");
  627. savage_do_cleanup_bci(dev);
  628. return -EINVAL;
  629. }
  630. drm_legacy_ioremap(dev->agp_buffer_map, dev);
  631. if (!dev->agp_buffer_map->handle) {
  632. DRM_ERROR("failed to ioremap DMA buffer region!\n");
  633. savage_do_cleanup_bci(dev);
  634. return -ENOMEM;
  635. }
  636. }
  637. if (init->agp_textures_offset) {
  638. dev_priv->agp_textures =
  639. drm_legacy_findmap(dev, init->agp_textures_offset);
  640. if (!dev_priv->agp_textures) {
  641. DRM_ERROR("could not find agp texture region!\n");
  642. savage_do_cleanup_bci(dev);
  643. return -EINVAL;
  644. }
  645. } else {
  646. dev_priv->agp_textures = NULL;
  647. }
  648. if (init->cmd_dma_offset) {
  649. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  650. DRM_ERROR("command DMA not supported on "
  651. "Savage3D/MX/IX.\n");
  652. savage_do_cleanup_bci(dev);
  653. return -EINVAL;
  654. }
  655. if (dev->dma && dev->dma->buflist) {
  656. DRM_ERROR("command and vertex DMA not supported "
  657. "at the same time.\n");
  658. savage_do_cleanup_bci(dev);
  659. return -EINVAL;
  660. }
  661. dev_priv->cmd_dma = drm_legacy_findmap(dev, init->cmd_dma_offset);
  662. if (!dev_priv->cmd_dma) {
  663. DRM_ERROR("could not find command DMA region!\n");
  664. savage_do_cleanup_bci(dev);
  665. return -EINVAL;
  666. }
  667. if (dev_priv->dma_type == SAVAGE_DMA_AGP) {
  668. if (dev_priv->cmd_dma->type != _DRM_AGP) {
  669. DRM_ERROR("AGP command DMA region is not a "
  670. "_DRM_AGP map!\n");
  671. savage_do_cleanup_bci(dev);
  672. return -EINVAL;
  673. }
  674. drm_legacy_ioremap(dev_priv->cmd_dma, dev);
  675. if (!dev_priv->cmd_dma->handle) {
  676. DRM_ERROR("failed to ioremap command "
  677. "DMA region!\n");
  678. savage_do_cleanup_bci(dev);
  679. return -ENOMEM;
  680. }
  681. } else if (dev_priv->cmd_dma->type != _DRM_CONSISTENT) {
  682. DRM_ERROR("PCI command DMA region is not a "
  683. "_DRM_CONSISTENT map!\n");
  684. savage_do_cleanup_bci(dev);
  685. return -EINVAL;
  686. }
  687. } else {
  688. dev_priv->cmd_dma = NULL;
  689. }
  690. dev_priv->dma_flush = savage_dma_flush;
  691. if (!dev_priv->cmd_dma) {
  692. DRM_DEBUG("falling back to faked command DMA.\n");
  693. dev_priv->fake_dma.offset = 0;
  694. dev_priv->fake_dma.size = SAVAGE_FAKE_DMA_SIZE;
  695. dev_priv->fake_dma.type = _DRM_SHM;
  696. dev_priv->fake_dma.handle = kmalloc(SAVAGE_FAKE_DMA_SIZE,
  697. GFP_KERNEL);
  698. if (!dev_priv->fake_dma.handle) {
  699. DRM_ERROR("could not allocate faked DMA buffer!\n");
  700. savage_do_cleanup_bci(dev);
  701. return -ENOMEM;
  702. }
  703. dev_priv->cmd_dma = &dev_priv->fake_dma;
  704. dev_priv->dma_flush = savage_fake_dma_flush;
  705. }
  706. dev_priv->sarea_priv =
  707. (drm_savage_sarea_t *) ((uint8_t *) dev_priv->sarea->handle +
  708. init->sarea_priv_offset);
  709. /* setup bitmap descriptors */
  710. {
  711. unsigned int color_tile_format;
  712. unsigned int depth_tile_format;
  713. unsigned int front_stride, back_stride, depth_stride;
  714. if (dev_priv->chipset <= S3_SAVAGE4) {
  715. color_tile_format = dev_priv->fb_bpp == 16 ?
  716. SAVAGE_BD_TILE_16BPP : SAVAGE_BD_TILE_32BPP;
  717. depth_tile_format = dev_priv->depth_bpp == 16 ?
  718. SAVAGE_BD_TILE_16BPP : SAVAGE_BD_TILE_32BPP;
  719. } else {
  720. color_tile_format = SAVAGE_BD_TILE_DEST;
  721. depth_tile_format = SAVAGE_BD_TILE_DEST;
  722. }
  723. front_stride = dev_priv->front_pitch / (dev_priv->fb_bpp / 8);
  724. back_stride = dev_priv->back_pitch / (dev_priv->fb_bpp / 8);
  725. depth_stride =
  726. dev_priv->depth_pitch / (dev_priv->depth_bpp / 8);
  727. dev_priv->front_bd = front_stride | SAVAGE_BD_BW_DISABLE |
  728. (dev_priv->fb_bpp << SAVAGE_BD_BPP_SHIFT) |
  729. (color_tile_format << SAVAGE_BD_TILE_SHIFT);
  730. dev_priv->back_bd = back_stride | SAVAGE_BD_BW_DISABLE |
  731. (dev_priv->fb_bpp << SAVAGE_BD_BPP_SHIFT) |
  732. (color_tile_format << SAVAGE_BD_TILE_SHIFT);
  733. dev_priv->depth_bd = depth_stride | SAVAGE_BD_BW_DISABLE |
  734. (dev_priv->depth_bpp << SAVAGE_BD_BPP_SHIFT) |
  735. (depth_tile_format << SAVAGE_BD_TILE_SHIFT);
  736. }
  737. /* setup status and bci ptr */
  738. dev_priv->event_counter = 0;
  739. dev_priv->event_wrap = 0;
  740. dev_priv->bci_ptr = (volatile uint32_t *)
  741. ((uint8_t *) dev_priv->mmio->handle + SAVAGE_BCI_OFFSET);
  742. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  743. dev_priv->status_used_mask = SAVAGE_FIFO_USED_MASK_S3D;
  744. } else {
  745. dev_priv->status_used_mask = SAVAGE_FIFO_USED_MASK_S4;
  746. }
  747. if (dev_priv->status != NULL) {
  748. dev_priv->status_ptr =
  749. (volatile uint32_t *)dev_priv->status->handle;
  750. dev_priv->wait_fifo = savage_bci_wait_fifo_shadow;
  751. dev_priv->wait_evnt = savage_bci_wait_event_shadow;
  752. dev_priv->status_ptr[1023] = dev_priv->event_counter;
  753. } else {
  754. dev_priv->status_ptr = NULL;
  755. if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
  756. dev_priv->wait_fifo = savage_bci_wait_fifo_s3d;
  757. } else {
  758. dev_priv->wait_fifo = savage_bci_wait_fifo_s4;
  759. }
  760. dev_priv->wait_evnt = savage_bci_wait_event_reg;
  761. }
  762. /* cliprect functions */
  763. if (S3_SAVAGE3D_SERIES(dev_priv->chipset))
  764. dev_priv->emit_clip_rect = savage_emit_clip_rect_s3d;
  765. else
  766. dev_priv->emit_clip_rect = savage_emit_clip_rect_s4;
  767. if (savage_freelist_init(dev) < 0) {
  768. DRM_ERROR("could not initialize freelist\n");
  769. savage_do_cleanup_bci(dev);
  770. return -ENOMEM;
  771. }
  772. if (savage_dma_init(dev_priv) < 0) {
  773. DRM_ERROR("could not initialize command DMA\n");
  774. savage_do_cleanup_bci(dev);
  775. return -ENOMEM;
  776. }
  777. return 0;
  778. }
  779. static int savage_do_cleanup_bci(struct drm_device * dev)
  780. {
  781. drm_savage_private_t *dev_priv = dev->dev_private;
  782. if (dev_priv->cmd_dma == &dev_priv->fake_dma) {
  783. kfree(dev_priv->fake_dma.handle);
  784. } else if (dev_priv->cmd_dma && dev_priv->cmd_dma->handle &&
  785. dev_priv->cmd_dma->type == _DRM_AGP &&
  786. dev_priv->dma_type == SAVAGE_DMA_AGP)
  787. drm_legacy_ioremapfree(dev_priv->cmd_dma, dev);
  788. if (dev_priv->dma_type == SAVAGE_DMA_AGP &&
  789. dev->agp_buffer_map && dev->agp_buffer_map->handle) {
  790. drm_legacy_ioremapfree(dev->agp_buffer_map, dev);
  791. /* make sure the next instance (which may be running
  792. * in PCI mode) doesn't try to use an old
  793. * agp_buffer_map. */
  794. dev->agp_buffer_map = NULL;
  795. }
  796. kfree(dev_priv->dma_pages);
  797. return 0;
  798. }
  799. static int savage_bci_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
  800. {
  801. drm_savage_init_t *init = data;
  802. LOCK_TEST_WITH_RETURN(dev, file_priv);
  803. switch (init->func) {
  804. case SAVAGE_INIT_BCI:
  805. return savage_do_init_bci(dev, init);
  806. case SAVAGE_CLEANUP_BCI:
  807. return savage_do_cleanup_bci(dev);
  808. }
  809. return -EINVAL;
  810. }
  811. static int savage_bci_event_emit(struct drm_device *dev, void *data, struct drm_file *file_priv)
  812. {
  813. drm_savage_private_t *dev_priv = dev->dev_private;
  814. drm_savage_event_emit_t *event = data;
  815. DRM_DEBUG("\n");
  816. LOCK_TEST_WITH_RETURN(dev, file_priv);
  817. event->count = savage_bci_emit_event(dev_priv, event->flags);
  818. event->count |= dev_priv->event_wrap << 16;
  819. return 0;
  820. }
  821. static int savage_bci_event_wait(struct drm_device *dev, void *data, struct drm_file *file_priv)
  822. {
  823. drm_savage_private_t *dev_priv = dev->dev_private;
  824. drm_savage_event_wait_t *event = data;
  825. unsigned int event_e, hw_e;
  826. unsigned int event_w, hw_w;
  827. DRM_DEBUG("\n");
  828. UPDATE_EVENT_COUNTER();
  829. if (dev_priv->status_ptr)
  830. hw_e = dev_priv->status_ptr[1] & 0xffff;
  831. else
  832. hw_e = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
  833. hw_w = dev_priv->event_wrap;
  834. if (hw_e > dev_priv->event_counter)
  835. hw_w--; /* hardware hasn't passed the last wrap yet */
  836. event_e = event->count & 0xffff;
  837. event_w = event->count >> 16;
  838. /* Don't need to wait if
  839. * - event counter wrapped since the event was emitted or
  840. * - the hardware has advanced up to or over the event to wait for.
  841. */
  842. if (event_w < hw_w || (event_w == hw_w && event_e <= hw_e))
  843. return 0;
  844. else
  845. return dev_priv->wait_evnt(dev_priv, event_e);
  846. }
  847. /*
  848. * DMA buffer management
  849. */
  850. static int savage_bci_get_buffers(struct drm_device *dev,
  851. struct drm_file *file_priv,
  852. struct drm_dma *d)
  853. {
  854. struct drm_buf *buf;
  855. int i;
  856. for (i = d->granted_count; i < d->request_count; i++) {
  857. buf = savage_freelist_get(dev);
  858. if (!buf)
  859. return -EAGAIN;
  860. buf->file_priv = file_priv;
  861. if (copy_to_user(&d->request_indices[i],
  862. &buf->idx, sizeof(buf->idx)))
  863. return -EFAULT;
  864. if (copy_to_user(&d->request_sizes[i],
  865. &buf->total, sizeof(buf->total)))
  866. return -EFAULT;
  867. d->granted_count++;
  868. }
  869. return 0;
  870. }
  871. int savage_bci_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
  872. {
  873. struct drm_device_dma *dma = dev->dma;
  874. struct drm_dma *d = data;
  875. int ret = 0;
  876. LOCK_TEST_WITH_RETURN(dev, file_priv);
  877. /* Please don't send us buffers.
  878. */
  879. if (d->send_count != 0) {
  880. DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
  881. DRM_CURRENTPID, d->send_count);
  882. return -EINVAL;
  883. }
  884. /* We'll send you buffers.
  885. */
  886. if (d->request_count < 0 || d->request_count > dma->buf_count) {
  887. DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
  888. DRM_CURRENTPID, d->request_count, dma->buf_count);
  889. return -EINVAL;
  890. }
  891. d->granted_count = 0;
  892. if (d->request_count) {
  893. ret = savage_bci_get_buffers(dev, file_priv, d);
  894. }
  895. return ret;
  896. }
  897. void savage_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv)
  898. {
  899. struct drm_device_dma *dma = dev->dma;
  900. drm_savage_private_t *dev_priv = dev->dev_private;
  901. int release_idlelock = 0;
  902. int i;
  903. if (!dma)
  904. return;
  905. if (!dev_priv)
  906. return;
  907. if (!dma->buflist)
  908. return;
  909. if (file_priv->master && file_priv->master->lock.hw_lock) {
  910. drm_legacy_idlelock_take(&file_priv->master->lock);
  911. release_idlelock = 1;
  912. }
  913. for (i = 0; i < dma->buf_count; i++) {
  914. struct drm_buf *buf = dma->buflist[i];
  915. drm_savage_buf_priv_t *buf_priv = buf->dev_private;
  916. if (buf->file_priv == file_priv && buf_priv &&
  917. buf_priv->next == NULL && buf_priv->prev == NULL) {
  918. uint16_t event;
  919. DRM_DEBUG("reclaimed from client\n");
  920. event = savage_bci_emit_event(dev_priv, SAVAGE_WAIT_3D);
  921. SET_AGE(&buf_priv->age, event, dev_priv->event_wrap);
  922. savage_freelist_put(dev, buf);
  923. }
  924. }
  925. if (release_idlelock)
  926. drm_legacy_idlelock_release(&file_priv->master->lock);
  927. }
  928. const struct drm_ioctl_desc savage_ioctls[] = {
  929. DRM_IOCTL_DEF_DRV(SAVAGE_BCI_INIT, savage_bci_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  930. DRM_IOCTL_DEF_DRV(SAVAGE_BCI_CMDBUF, savage_bci_cmdbuf, DRM_AUTH),
  931. DRM_IOCTL_DEF_DRV(SAVAGE_BCI_EVENT_EMIT, savage_bci_event_emit, DRM_AUTH),
  932. DRM_IOCTL_DEF_DRV(SAVAGE_BCI_EVENT_WAIT, savage_bci_event_wait, DRM_AUTH),
  933. };
  934. int savage_max_ioctl = ARRAY_SIZE(savage_ioctls);