vmwgfx_cmdbuf.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. /**************************************************************************
  2. *
  3. * Copyright © 2015 VMware, Inc., Palo Alto, CA., USA
  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
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include <drm/ttm/ttm_bo_api.h>
  28. #include "vmwgfx_drv.h"
  29. /*
  30. * Size of inline command buffers. Try to make sure that a page size is a
  31. * multiple of the DMA pool allocation size.
  32. */
  33. #define VMW_CMDBUF_INLINE_ALIGN 64
  34. #define VMW_CMDBUF_INLINE_SIZE \
  35. (1024 - ALIGN(sizeof(SVGACBHeader), VMW_CMDBUF_INLINE_ALIGN))
  36. /**
  37. * struct vmw_cmdbuf_context - Command buffer context queues
  38. *
  39. * @submitted: List of command buffers that have been submitted to the
  40. * manager but not yet submitted to hardware.
  41. * @hw_submitted: List of command buffers submitted to hardware.
  42. * @preempted: List of preempted command buffers.
  43. * @num_hw_submitted: Number of buffers currently being processed by hardware
  44. */
  45. struct vmw_cmdbuf_context {
  46. struct list_head submitted;
  47. struct list_head hw_submitted;
  48. struct list_head preempted;
  49. unsigned num_hw_submitted;
  50. bool block_submission;
  51. };
  52. /**
  53. * struct vmw_cmdbuf_man: - Command buffer manager
  54. *
  55. * @cur_mutex: Mutex protecting the command buffer used for incremental small
  56. * kernel command submissions, @cur.
  57. * @space_mutex: Mutex to protect against starvation when we allocate
  58. * main pool buffer space.
  59. * @error_mutex: Mutex to serialize the work queue error handling.
  60. * Note this is not needed if the same workqueue handler
  61. * can't race with itself...
  62. * @work: A struct work_struct implementeing command buffer error handling.
  63. * Immutable.
  64. * @dev_priv: Pointer to the device private struct. Immutable.
  65. * @ctx: Array of command buffer context queues. The queues and the context
  66. * data is protected by @lock.
  67. * @error: List of command buffers that have caused device errors.
  68. * Protected by @lock.
  69. * @mm: Range manager for the command buffer space. Manager allocations and
  70. * frees are protected by @lock.
  71. * @cmd_space: Buffer object for the command buffer space, unless we were
  72. * able to make a contigous coherent DMA memory allocation, @handle. Immutable.
  73. * @map_obj: Mapping state for @cmd_space. Immutable.
  74. * @map: Pointer to command buffer space. May be a mapped buffer object or
  75. * a contigous coherent DMA memory allocation. Immutable.
  76. * @cur: Command buffer for small kernel command submissions. Protected by
  77. * the @cur_mutex.
  78. * @cur_pos: Space already used in @cur. Protected by @cur_mutex.
  79. * @default_size: Default size for the @cur command buffer. Immutable.
  80. * @max_hw_submitted: Max number of in-flight command buffers the device can
  81. * handle. Immutable.
  82. * @lock: Spinlock protecting command submission queues.
  83. * @header: Pool of DMA memory for device command buffer headers.
  84. * Internal protection.
  85. * @dheaders: Pool of DMA memory for device command buffer headers with trailing
  86. * space for inline data. Internal protection.
  87. * @alloc_queue: Wait queue for processes waiting to allocate command buffer
  88. * space.
  89. * @idle_queue: Wait queue for processes waiting for command buffer idle.
  90. * @irq_on: Whether the process function has requested irq to be turned on.
  91. * Protected by @lock.
  92. * @using_mob: Whether the command buffer space is a MOB or a contigous DMA
  93. * allocation. Immutable.
  94. * @has_pool: Has a large pool of DMA memory which allows larger allocations.
  95. * Typically this is false only during bootstrap.
  96. * @handle: DMA address handle for the command buffer space if @using_mob is
  97. * false. Immutable.
  98. * @size: The size of the command buffer space. Immutable.
  99. */
  100. struct vmw_cmdbuf_man {
  101. struct mutex cur_mutex;
  102. struct mutex space_mutex;
  103. struct mutex error_mutex;
  104. struct work_struct work;
  105. struct vmw_private *dev_priv;
  106. struct vmw_cmdbuf_context ctx[SVGA_CB_CONTEXT_MAX];
  107. struct list_head error;
  108. struct drm_mm mm;
  109. struct ttm_buffer_object *cmd_space;
  110. struct ttm_bo_kmap_obj map_obj;
  111. u8 *map;
  112. struct vmw_cmdbuf_header *cur;
  113. size_t cur_pos;
  114. size_t default_size;
  115. unsigned max_hw_submitted;
  116. spinlock_t lock;
  117. struct dma_pool *headers;
  118. struct dma_pool *dheaders;
  119. wait_queue_head_t alloc_queue;
  120. wait_queue_head_t idle_queue;
  121. bool irq_on;
  122. bool using_mob;
  123. bool has_pool;
  124. dma_addr_t handle;
  125. size_t size;
  126. };
  127. /**
  128. * struct vmw_cmdbuf_header - Command buffer metadata
  129. *
  130. * @man: The command buffer manager.
  131. * @cb_header: Device command buffer header, allocated from a DMA pool.
  132. * @cb_context: The device command buffer context.
  133. * @list: List head for attaching to the manager lists.
  134. * @node: The range manager node.
  135. * @handle. The DMA address of @cb_header. Handed to the device on command
  136. * buffer submission.
  137. * @cmd: Pointer to the command buffer space of this buffer.
  138. * @size: Size of the command buffer space of this buffer.
  139. * @reserved: Reserved space of this buffer.
  140. * @inline_space: Whether inline command buffer space is used.
  141. */
  142. struct vmw_cmdbuf_header {
  143. struct vmw_cmdbuf_man *man;
  144. SVGACBHeader *cb_header;
  145. SVGACBContext cb_context;
  146. struct list_head list;
  147. struct drm_mm_node node;
  148. dma_addr_t handle;
  149. u8 *cmd;
  150. size_t size;
  151. size_t reserved;
  152. bool inline_space;
  153. };
  154. /**
  155. * struct vmw_cmdbuf_dheader - Device command buffer header with inline
  156. * command buffer space.
  157. *
  158. * @cb_header: Device command buffer header.
  159. * @cmd: Inline command buffer space.
  160. */
  161. struct vmw_cmdbuf_dheader {
  162. SVGACBHeader cb_header;
  163. u8 cmd[VMW_CMDBUF_INLINE_SIZE] __aligned(VMW_CMDBUF_INLINE_ALIGN);
  164. };
  165. /**
  166. * struct vmw_cmdbuf_alloc_info - Command buffer space allocation metadata
  167. *
  168. * @page_size: Size of requested command buffer space in pages.
  169. * @node: Pointer to the range manager node.
  170. * @done: True if this allocation has succeeded.
  171. */
  172. struct vmw_cmdbuf_alloc_info {
  173. size_t page_size;
  174. struct drm_mm_node *node;
  175. bool done;
  176. };
  177. /* Loop over each context in the command buffer manager. */
  178. #define for_each_cmdbuf_ctx(_man, _i, _ctx) \
  179. for (_i = 0, _ctx = &(_man)->ctx[0]; (_i) < SVGA_CB_CONTEXT_MAX; \
  180. ++(_i), ++(_ctx))
  181. static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, u32 context,
  182. bool enable);
  183. static int vmw_cmdbuf_preempt(struct vmw_cmdbuf_man *man, u32 context);
  184. /**
  185. * vmw_cmdbuf_cur_lock - Helper to lock the cur_mutex.
  186. *
  187. * @man: The range manager.
  188. * @interruptible: Whether to wait interruptible when locking.
  189. */
  190. static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man *man, bool interruptible)
  191. {
  192. if (interruptible) {
  193. if (mutex_lock_interruptible(&man->cur_mutex))
  194. return -ERESTARTSYS;
  195. } else {
  196. mutex_lock(&man->cur_mutex);
  197. }
  198. return 0;
  199. }
  200. /**
  201. * vmw_cmdbuf_cur_unlock - Helper to unlock the cur_mutex.
  202. *
  203. * @man: The range manager.
  204. */
  205. static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man *man)
  206. {
  207. mutex_unlock(&man->cur_mutex);
  208. }
  209. /**
  210. * vmw_cmdbuf_header_inline_free - Free a struct vmw_cmdbuf_header that has
  211. * been used for the device context with inline command buffers.
  212. * Need not be called locked.
  213. *
  214. * @header: Pointer to the header to free.
  215. */
  216. static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header *header)
  217. {
  218. struct vmw_cmdbuf_dheader *dheader;
  219. if (WARN_ON_ONCE(!header->inline_space))
  220. return;
  221. dheader = container_of(header->cb_header, struct vmw_cmdbuf_dheader,
  222. cb_header);
  223. dma_pool_free(header->man->dheaders, dheader, header->handle);
  224. kfree(header);
  225. }
  226. /**
  227. * __vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
  228. * associated structures.
  229. *
  230. * header: Pointer to the header to free.
  231. *
  232. * For internal use. Must be called with man::lock held.
  233. */
  234. static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
  235. {
  236. struct vmw_cmdbuf_man *man = header->man;
  237. lockdep_assert_held_once(&man->lock);
  238. if (header->inline_space) {
  239. vmw_cmdbuf_header_inline_free(header);
  240. return;
  241. }
  242. drm_mm_remove_node(&header->node);
  243. wake_up_all(&man->alloc_queue);
  244. if (header->cb_header)
  245. dma_pool_free(man->headers, header->cb_header,
  246. header->handle);
  247. kfree(header);
  248. }
  249. /**
  250. * vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
  251. * associated structures.
  252. *
  253. * @header: Pointer to the header to free.
  254. */
  255. void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
  256. {
  257. struct vmw_cmdbuf_man *man = header->man;
  258. /* Avoid locking if inline_space */
  259. if (header->inline_space) {
  260. vmw_cmdbuf_header_inline_free(header);
  261. return;
  262. }
  263. spin_lock(&man->lock);
  264. __vmw_cmdbuf_header_free(header);
  265. spin_unlock(&man->lock);
  266. }
  267. /**
  268. * vmw_cmbuf_header_submit: Submit a command buffer to hardware.
  269. *
  270. * @header: The header of the buffer to submit.
  271. */
  272. static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header *header)
  273. {
  274. struct vmw_cmdbuf_man *man = header->man;
  275. u32 val;
  276. val = upper_32_bits(header->handle);
  277. vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val);
  278. val = lower_32_bits(header->handle);
  279. val |= header->cb_context & SVGA_CB_CONTEXT_MASK;
  280. vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val);
  281. return header->cb_header->status;
  282. }
  283. /**
  284. * vmw_cmdbuf_ctx_init: Initialize a command buffer context.
  285. *
  286. * @ctx: The command buffer context to initialize
  287. */
  288. static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx)
  289. {
  290. INIT_LIST_HEAD(&ctx->hw_submitted);
  291. INIT_LIST_HEAD(&ctx->submitted);
  292. INIT_LIST_HEAD(&ctx->preempted);
  293. ctx->num_hw_submitted = 0;
  294. }
  295. /**
  296. * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer
  297. * context.
  298. *
  299. * @man: The command buffer manager.
  300. * @ctx: The command buffer context.
  301. *
  302. * Submits command buffers to hardware until there are no more command
  303. * buffers to submit or the hardware can't handle more command buffers.
  304. */
  305. static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man,
  306. struct vmw_cmdbuf_context *ctx)
  307. {
  308. while (ctx->num_hw_submitted < man->max_hw_submitted &&
  309. !list_empty(&ctx->submitted) &&
  310. !ctx->block_submission) {
  311. struct vmw_cmdbuf_header *entry;
  312. SVGACBStatus status;
  313. entry = list_first_entry(&ctx->submitted,
  314. struct vmw_cmdbuf_header,
  315. list);
  316. status = vmw_cmdbuf_header_submit(entry);
  317. /* This should never happen */
  318. if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) {
  319. entry->cb_header->status = SVGA_CB_STATUS_NONE;
  320. break;
  321. }
  322. list_del(&entry->list);
  323. list_add_tail(&entry->list, &ctx->hw_submitted);
  324. ctx->num_hw_submitted++;
  325. }
  326. }
  327. /**
  328. * vmw_cmdbuf_ctx_submit: Process a command buffer context.
  329. *
  330. * @man: The command buffer manager.
  331. * @ctx: The command buffer context.
  332. *
  333. * Submit command buffers to hardware if possible, and process finished
  334. * buffers. Typically freeing them, but on preemption or error take
  335. * appropriate action. Wake up waiters if appropriate.
  336. */
  337. static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man,
  338. struct vmw_cmdbuf_context *ctx,
  339. int *notempty)
  340. {
  341. struct vmw_cmdbuf_header *entry, *next;
  342. vmw_cmdbuf_ctx_submit(man, ctx);
  343. list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) {
  344. SVGACBStatus status = entry->cb_header->status;
  345. if (status == SVGA_CB_STATUS_NONE)
  346. break;
  347. list_del(&entry->list);
  348. wake_up_all(&man->idle_queue);
  349. ctx->num_hw_submitted--;
  350. switch (status) {
  351. case SVGA_CB_STATUS_COMPLETED:
  352. __vmw_cmdbuf_header_free(entry);
  353. break;
  354. case SVGA_CB_STATUS_COMMAND_ERROR:
  355. entry->cb_header->status = SVGA_CB_STATUS_NONE;
  356. list_add_tail(&entry->list, &man->error);
  357. schedule_work(&man->work);
  358. break;
  359. case SVGA_CB_STATUS_PREEMPTED:
  360. entry->cb_header->status = SVGA_CB_STATUS_NONE;
  361. list_add_tail(&entry->list, &ctx->preempted);
  362. break;
  363. case SVGA_CB_STATUS_CB_HEADER_ERROR:
  364. WARN_ONCE(true, "Command buffer header error.\n");
  365. __vmw_cmdbuf_header_free(entry);
  366. break;
  367. default:
  368. WARN_ONCE(true, "Undefined command buffer status.\n");
  369. __vmw_cmdbuf_header_free(entry);
  370. break;
  371. }
  372. }
  373. vmw_cmdbuf_ctx_submit(man, ctx);
  374. if (!list_empty(&ctx->submitted))
  375. (*notempty)++;
  376. }
  377. /**
  378. * vmw_cmdbuf_man_process - Process all command buffer contexts and
  379. * switch on and off irqs as appropriate.
  380. *
  381. * @man: The command buffer manager.
  382. *
  383. * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has
  384. * command buffers left that are not submitted to hardware, Make sure
  385. * IRQ handling is turned on. Otherwise, make sure it's turned off.
  386. */
  387. static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man)
  388. {
  389. int notempty;
  390. struct vmw_cmdbuf_context *ctx;
  391. int i;
  392. retry:
  393. notempty = 0;
  394. for_each_cmdbuf_ctx(man, i, ctx)
  395. vmw_cmdbuf_ctx_process(man, ctx, &notempty);
  396. if (man->irq_on && !notempty) {
  397. vmw_generic_waiter_remove(man->dev_priv,
  398. SVGA_IRQFLAG_COMMAND_BUFFER,
  399. &man->dev_priv->cmdbuf_waiters);
  400. man->irq_on = false;
  401. } else if (!man->irq_on && notempty) {
  402. vmw_generic_waiter_add(man->dev_priv,
  403. SVGA_IRQFLAG_COMMAND_BUFFER,
  404. &man->dev_priv->cmdbuf_waiters);
  405. man->irq_on = true;
  406. /* Rerun in case we just missed an irq. */
  407. goto retry;
  408. }
  409. }
  410. /**
  411. * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a
  412. * command buffer context
  413. *
  414. * @man: The command buffer manager.
  415. * @header: The header of the buffer to submit.
  416. * @cb_context: The command buffer context to use.
  417. *
  418. * This function adds @header to the "submitted" queue of the command
  419. * buffer context identified by @cb_context. It then calls the command buffer
  420. * manager processing to potentially submit the buffer to hardware.
  421. * @man->lock needs to be held when calling this function.
  422. */
  423. static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man,
  424. struct vmw_cmdbuf_header *header,
  425. SVGACBContext cb_context)
  426. {
  427. if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT))
  428. header->cb_header->dxContext = 0;
  429. header->cb_context = cb_context;
  430. list_add_tail(&header->list, &man->ctx[cb_context].submitted);
  431. vmw_cmdbuf_man_process(man);
  432. }
  433. /**
  434. * vmw_cmdbuf_irqthread - The main part of the command buffer interrupt
  435. * handler implemented as a threaded irq task.
  436. *
  437. * @man: Pointer to the command buffer manager.
  438. *
  439. * The bottom half of the interrupt handler simply calls into the
  440. * command buffer processor to free finished buffers and submit any
  441. * queued buffers to hardware.
  442. */
  443. void vmw_cmdbuf_irqthread(struct vmw_cmdbuf_man *man)
  444. {
  445. spin_lock(&man->lock);
  446. vmw_cmdbuf_man_process(man);
  447. spin_unlock(&man->lock);
  448. }
  449. /**
  450. * vmw_cmdbuf_work_func - The deferred work function that handles
  451. * command buffer errors.
  452. *
  453. * @work: The work func closure argument.
  454. *
  455. * Restarting the command buffer context after an error requires process
  456. * context, so it is deferred to this work function.
  457. */
  458. static void vmw_cmdbuf_work_func(struct work_struct *work)
  459. {
  460. struct vmw_cmdbuf_man *man =
  461. container_of(work, struct vmw_cmdbuf_man, work);
  462. struct vmw_cmdbuf_header *entry, *next;
  463. uint32_t dummy;
  464. bool restart[SVGA_CB_CONTEXT_MAX];
  465. bool send_fence = false;
  466. struct list_head restart_head[SVGA_CB_CONTEXT_MAX];
  467. int i;
  468. struct vmw_cmdbuf_context *ctx;
  469. for_each_cmdbuf_ctx(man, i, ctx) {
  470. INIT_LIST_HEAD(&restart_head[i]);
  471. restart[i] = false;
  472. }
  473. mutex_lock(&man->error_mutex);
  474. spin_lock(&man->lock);
  475. list_for_each_entry_safe(entry, next, &man->error, list) {
  476. SVGACBHeader *cb_hdr = entry->cb_header;
  477. SVGA3dCmdHeader *header = (SVGA3dCmdHeader *)
  478. (entry->cmd + cb_hdr->errorOffset);
  479. u32 error_cmd_size, new_start_offset;
  480. const char *cmd_name;
  481. list_del_init(&entry->list);
  482. restart[entry->cb_context] = true;
  483. if (!vmw_cmd_describe(header, &error_cmd_size, &cmd_name)) {
  484. DRM_ERROR("Unknown command causing device error.\n");
  485. DRM_ERROR("Command buffer offset is %lu\n",
  486. (unsigned long) cb_hdr->errorOffset);
  487. __vmw_cmdbuf_header_free(entry);
  488. send_fence = true;
  489. continue;
  490. }
  491. DRM_ERROR("Command \"%s\" causing device error.\n", cmd_name);
  492. DRM_ERROR("Command buffer offset is %lu\n",
  493. (unsigned long) cb_hdr->errorOffset);
  494. DRM_ERROR("Command size is %lu\n",
  495. (unsigned long) error_cmd_size);
  496. new_start_offset = cb_hdr->errorOffset + error_cmd_size;
  497. if (new_start_offset >= cb_hdr->length) {
  498. __vmw_cmdbuf_header_free(entry);
  499. send_fence = true;
  500. continue;
  501. }
  502. if (man->using_mob)
  503. cb_hdr->ptr.mob.mobOffset += new_start_offset;
  504. else
  505. cb_hdr->ptr.pa += (u64) new_start_offset;
  506. entry->cmd += new_start_offset;
  507. cb_hdr->length -= new_start_offset;
  508. cb_hdr->errorOffset = 0;
  509. cb_hdr->offset = 0;
  510. list_add_tail(&entry->list, &restart_head[entry->cb_context]);
  511. man->ctx[entry->cb_context].block_submission = true;
  512. }
  513. spin_unlock(&man->lock);
  514. /* Preempt all contexts with errors */
  515. for_each_cmdbuf_ctx(man, i, ctx) {
  516. if (ctx->block_submission && vmw_cmdbuf_preempt(man, i))
  517. DRM_ERROR("Failed preempting command buffer "
  518. "context %u.\n", i);
  519. }
  520. spin_lock(&man->lock);
  521. for_each_cmdbuf_ctx(man, i, ctx) {
  522. if (!ctx->block_submission)
  523. continue;
  524. /* Move preempted command buffers to the preempted queue. */
  525. vmw_cmdbuf_ctx_process(man, ctx, &dummy);
  526. /*
  527. * Add the preempted queue after the command buffer
  528. * that caused an error.
  529. */
  530. list_splice_init(&ctx->preempted, restart_head[i].prev);
  531. /*
  532. * Finally add all command buffers first in the submitted
  533. * queue, to rerun them.
  534. */
  535. list_splice_init(&restart_head[i], &ctx->submitted);
  536. ctx->block_submission = false;
  537. }
  538. vmw_cmdbuf_man_process(man);
  539. spin_unlock(&man->lock);
  540. for_each_cmdbuf_ctx(man, i, ctx) {
  541. if (restart[i] && vmw_cmdbuf_startstop(man, i, true))
  542. DRM_ERROR("Failed restarting command buffer "
  543. "context %u.\n", i);
  544. }
  545. /* Send a new fence in case one was removed */
  546. if (send_fence) {
  547. vmw_fifo_send_fence(man->dev_priv, &dummy);
  548. wake_up_all(&man->idle_queue);
  549. }
  550. mutex_unlock(&man->error_mutex);
  551. }
  552. /**
  553. * vmw_cmdbuf_man idle - Check whether the command buffer manager is idle.
  554. *
  555. * @man: The command buffer manager.
  556. * @check_preempted: Check also the preempted queue for pending command buffers.
  557. *
  558. */
  559. static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man,
  560. bool check_preempted)
  561. {
  562. struct vmw_cmdbuf_context *ctx;
  563. bool idle = false;
  564. int i;
  565. spin_lock(&man->lock);
  566. vmw_cmdbuf_man_process(man);
  567. for_each_cmdbuf_ctx(man, i, ctx) {
  568. if (!list_empty(&ctx->submitted) ||
  569. !list_empty(&ctx->hw_submitted) ||
  570. (check_preempted && !list_empty(&ctx->preempted)))
  571. goto out_unlock;
  572. }
  573. idle = list_empty(&man->error);
  574. out_unlock:
  575. spin_unlock(&man->lock);
  576. return idle;
  577. }
  578. /**
  579. * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  580. * command submissions
  581. *
  582. * @man: The command buffer manager.
  583. *
  584. * Flushes the current command buffer without allocating a new one. A new one
  585. * is automatically allocated when needed. Call with @man->cur_mutex held.
  586. */
  587. static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man)
  588. {
  589. struct vmw_cmdbuf_header *cur = man->cur;
  590. WARN_ON(!mutex_is_locked(&man->cur_mutex));
  591. if (!cur)
  592. return;
  593. spin_lock(&man->lock);
  594. if (man->cur_pos == 0) {
  595. __vmw_cmdbuf_header_free(cur);
  596. goto out_unlock;
  597. }
  598. man->cur->cb_header->length = man->cur_pos;
  599. vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0);
  600. out_unlock:
  601. spin_unlock(&man->lock);
  602. man->cur = NULL;
  603. man->cur_pos = 0;
  604. }
  605. /**
  606. * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  607. * command submissions
  608. *
  609. * @man: The command buffer manager.
  610. * @interruptible: Whether to sleep interruptible when sleeping.
  611. *
  612. * Flushes the current command buffer without allocating a new one. A new one
  613. * is automatically allocated when needed.
  614. */
  615. int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man,
  616. bool interruptible)
  617. {
  618. int ret = vmw_cmdbuf_cur_lock(man, interruptible);
  619. if (ret)
  620. return ret;
  621. __vmw_cmdbuf_cur_flush(man);
  622. vmw_cmdbuf_cur_unlock(man);
  623. return 0;
  624. }
  625. /**
  626. * vmw_cmdbuf_idle - Wait for command buffer manager idle.
  627. *
  628. * @man: The command buffer manager.
  629. * @interruptible: Sleep interruptible while waiting.
  630. * @timeout: Time out after this many ticks.
  631. *
  632. * Wait until the command buffer manager has processed all command buffers,
  633. * or until a timeout occurs. If a timeout occurs, the function will return
  634. * -EBUSY.
  635. */
  636. int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible,
  637. unsigned long timeout)
  638. {
  639. int ret;
  640. ret = vmw_cmdbuf_cur_flush(man, interruptible);
  641. vmw_generic_waiter_add(man->dev_priv,
  642. SVGA_IRQFLAG_COMMAND_BUFFER,
  643. &man->dev_priv->cmdbuf_waiters);
  644. if (interruptible) {
  645. ret = wait_event_interruptible_timeout
  646. (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  647. timeout);
  648. } else {
  649. ret = wait_event_timeout
  650. (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  651. timeout);
  652. }
  653. vmw_generic_waiter_remove(man->dev_priv,
  654. SVGA_IRQFLAG_COMMAND_BUFFER,
  655. &man->dev_priv->cmdbuf_waiters);
  656. if (ret == 0) {
  657. if (!vmw_cmdbuf_man_idle(man, true))
  658. ret = -EBUSY;
  659. else
  660. ret = 0;
  661. }
  662. if (ret > 0)
  663. ret = 0;
  664. return ret;
  665. }
  666. /**
  667. * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool.
  668. *
  669. * @man: The command buffer manager.
  670. * @info: Allocation info. Will hold the size on entry and allocated mm node
  671. * on successful return.
  672. *
  673. * Try to allocate buffer space from the main pool. Returns true if succeeded.
  674. * If a fatal error was hit, the error code is returned in @info->ret.
  675. */
  676. static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man,
  677. struct vmw_cmdbuf_alloc_info *info)
  678. {
  679. int ret;
  680. if (info->done)
  681. return true;
  682. memset(info->node, 0, sizeof(*info->node));
  683. spin_lock(&man->lock);
  684. ret = drm_mm_insert_node(&man->mm, info->node, info->page_size);
  685. if (ret) {
  686. vmw_cmdbuf_man_process(man);
  687. ret = drm_mm_insert_node(&man->mm, info->node, info->page_size);
  688. }
  689. spin_unlock(&man->lock);
  690. info->done = !ret;
  691. return info->done;
  692. }
  693. /**
  694. * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool.
  695. *
  696. * @man: The command buffer manager.
  697. * @node: Pointer to pre-allocated range-manager node.
  698. * @size: The size of the allocation.
  699. * @interruptible: Whether to sleep interruptible while waiting for space.
  700. *
  701. * This function allocates buffer space from the main pool, and if there is
  702. * no space available ATM, it turns on IRQ handling and sleeps waiting for it to
  703. * become available.
  704. */
  705. static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man,
  706. struct drm_mm_node *node,
  707. size_t size,
  708. bool interruptible)
  709. {
  710. struct vmw_cmdbuf_alloc_info info;
  711. info.page_size = PAGE_ALIGN(size) >> PAGE_SHIFT;
  712. info.node = node;
  713. info.done = false;
  714. /*
  715. * To prevent starvation of large requests, only one allocating call
  716. * at a time waiting for space.
  717. */
  718. if (interruptible) {
  719. if (mutex_lock_interruptible(&man->space_mutex))
  720. return -ERESTARTSYS;
  721. } else {
  722. mutex_lock(&man->space_mutex);
  723. }
  724. /* Try to allocate space without waiting. */
  725. if (vmw_cmdbuf_try_alloc(man, &info))
  726. goto out_unlock;
  727. vmw_generic_waiter_add(man->dev_priv,
  728. SVGA_IRQFLAG_COMMAND_BUFFER,
  729. &man->dev_priv->cmdbuf_waiters);
  730. if (interruptible) {
  731. int ret;
  732. ret = wait_event_interruptible
  733. (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  734. if (ret) {
  735. vmw_generic_waiter_remove
  736. (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER,
  737. &man->dev_priv->cmdbuf_waiters);
  738. mutex_unlock(&man->space_mutex);
  739. return ret;
  740. }
  741. } else {
  742. wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  743. }
  744. vmw_generic_waiter_remove(man->dev_priv,
  745. SVGA_IRQFLAG_COMMAND_BUFFER,
  746. &man->dev_priv->cmdbuf_waiters);
  747. out_unlock:
  748. mutex_unlock(&man->space_mutex);
  749. return 0;
  750. }
  751. /**
  752. * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer
  753. * space from the main pool.
  754. *
  755. * @man: The command buffer manager.
  756. * @header: Pointer to the header to set up.
  757. * @size: The requested size of the buffer space.
  758. * @interruptible: Whether to sleep interruptible while waiting for space.
  759. */
  760. static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man,
  761. struct vmw_cmdbuf_header *header,
  762. size_t size,
  763. bool interruptible)
  764. {
  765. SVGACBHeader *cb_hdr;
  766. size_t offset;
  767. int ret;
  768. if (!man->has_pool)
  769. return -ENOMEM;
  770. ret = vmw_cmdbuf_alloc_space(man, &header->node, size, interruptible);
  771. if (ret)
  772. return ret;
  773. header->cb_header = dma_pool_zalloc(man->headers, GFP_KERNEL,
  774. &header->handle);
  775. if (!header->cb_header) {
  776. ret = -ENOMEM;
  777. goto out_no_cb_header;
  778. }
  779. header->size = header->node.size << PAGE_SHIFT;
  780. cb_hdr = header->cb_header;
  781. offset = header->node.start << PAGE_SHIFT;
  782. header->cmd = man->map + offset;
  783. if (man->using_mob) {
  784. cb_hdr->flags = SVGA_CB_FLAG_MOB;
  785. cb_hdr->ptr.mob.mobid = man->cmd_space->mem.start;
  786. cb_hdr->ptr.mob.mobOffset = offset;
  787. } else {
  788. cb_hdr->ptr.pa = (u64)man->handle + (u64)offset;
  789. }
  790. return 0;
  791. out_no_cb_header:
  792. spin_lock(&man->lock);
  793. drm_mm_remove_node(&header->node);
  794. spin_unlock(&man->lock);
  795. return ret;
  796. }
  797. /**
  798. * vmw_cmdbuf_space_inline - Set up a command buffer header with
  799. * inline command buffer space.
  800. *
  801. * @man: The command buffer manager.
  802. * @header: Pointer to the header to set up.
  803. * @size: The requested size of the buffer space.
  804. */
  805. static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man,
  806. struct vmw_cmdbuf_header *header,
  807. int size)
  808. {
  809. struct vmw_cmdbuf_dheader *dheader;
  810. SVGACBHeader *cb_hdr;
  811. if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE))
  812. return -ENOMEM;
  813. dheader = dma_pool_zalloc(man->dheaders, GFP_KERNEL,
  814. &header->handle);
  815. if (!dheader)
  816. return -ENOMEM;
  817. header->inline_space = true;
  818. header->size = VMW_CMDBUF_INLINE_SIZE;
  819. cb_hdr = &dheader->cb_header;
  820. header->cb_header = cb_hdr;
  821. header->cmd = dheader->cmd;
  822. cb_hdr->status = SVGA_CB_STATUS_NONE;
  823. cb_hdr->flags = SVGA_CB_FLAG_NONE;
  824. cb_hdr->ptr.pa = (u64)header->handle +
  825. (u64)offsetof(struct vmw_cmdbuf_dheader, cmd);
  826. return 0;
  827. }
  828. /**
  829. * vmw_cmdbuf_alloc - Allocate a command buffer header complete with
  830. * command buffer space.
  831. *
  832. * @man: The command buffer manager.
  833. * @size: The requested size of the buffer space.
  834. * @interruptible: Whether to sleep interruptible while waiting for space.
  835. * @p_header: points to a header pointer to populate on successful return.
  836. *
  837. * Returns a pointer to command buffer space if successful. Otherwise
  838. * returns an error pointer. The header pointer returned in @p_header should
  839. * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit().
  840. */
  841. void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man,
  842. size_t size, bool interruptible,
  843. struct vmw_cmdbuf_header **p_header)
  844. {
  845. struct vmw_cmdbuf_header *header;
  846. int ret = 0;
  847. *p_header = NULL;
  848. header = kzalloc(sizeof(*header), GFP_KERNEL);
  849. if (!header)
  850. return ERR_PTR(-ENOMEM);
  851. if (size <= VMW_CMDBUF_INLINE_SIZE)
  852. ret = vmw_cmdbuf_space_inline(man, header, size);
  853. else
  854. ret = vmw_cmdbuf_space_pool(man, header, size, interruptible);
  855. if (ret) {
  856. kfree(header);
  857. return ERR_PTR(ret);
  858. }
  859. header->man = man;
  860. INIT_LIST_HEAD(&header->list);
  861. header->cb_header->status = SVGA_CB_STATUS_NONE;
  862. *p_header = header;
  863. return header->cmd;
  864. }
  865. /**
  866. * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current
  867. * command buffer.
  868. *
  869. * @man: The command buffer manager.
  870. * @size: The requested size of the commands.
  871. * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  872. * @interruptible: Whether to sleep interruptible while waiting for space.
  873. *
  874. * Returns a pointer to command buffer space if successful. Otherwise
  875. * returns an error pointer.
  876. */
  877. static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man,
  878. size_t size,
  879. int ctx_id,
  880. bool interruptible)
  881. {
  882. struct vmw_cmdbuf_header *cur;
  883. void *ret;
  884. if (vmw_cmdbuf_cur_lock(man, interruptible))
  885. return ERR_PTR(-ERESTARTSYS);
  886. cur = man->cur;
  887. if (cur && (size + man->cur_pos > cur->size ||
  888. ((cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) &&
  889. ctx_id != cur->cb_header->dxContext)))
  890. __vmw_cmdbuf_cur_flush(man);
  891. if (!man->cur) {
  892. ret = vmw_cmdbuf_alloc(man,
  893. max_t(size_t, size, man->default_size),
  894. interruptible, &man->cur);
  895. if (IS_ERR(ret)) {
  896. vmw_cmdbuf_cur_unlock(man);
  897. return ret;
  898. }
  899. cur = man->cur;
  900. }
  901. if (ctx_id != SVGA3D_INVALID_ID) {
  902. cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  903. cur->cb_header->dxContext = ctx_id;
  904. }
  905. cur->reserved = size;
  906. return (void *) (man->cur->cmd + man->cur_pos);
  907. }
  908. /**
  909. * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer.
  910. *
  911. * @man: The command buffer manager.
  912. * @size: The size of the commands actually written.
  913. * @flush: Whether to flush the command buffer immediately.
  914. */
  915. static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man,
  916. size_t size, bool flush)
  917. {
  918. struct vmw_cmdbuf_header *cur = man->cur;
  919. WARN_ON(!mutex_is_locked(&man->cur_mutex));
  920. WARN_ON(size > cur->reserved);
  921. man->cur_pos += size;
  922. if (!size)
  923. cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  924. if (flush)
  925. __vmw_cmdbuf_cur_flush(man);
  926. vmw_cmdbuf_cur_unlock(man);
  927. }
  928. /**
  929. * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer.
  930. *
  931. * @man: The command buffer manager.
  932. * @size: The requested size of the commands.
  933. * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  934. * @interruptible: Whether to sleep interruptible while waiting for space.
  935. * @header: Header of the command buffer. NULL if the current command buffer
  936. * should be used.
  937. *
  938. * Returns a pointer to command buffer space if successful. Otherwise
  939. * returns an error pointer.
  940. */
  941. void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
  942. int ctx_id, bool interruptible,
  943. struct vmw_cmdbuf_header *header)
  944. {
  945. if (!header)
  946. return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible);
  947. if (size > header->size)
  948. return ERR_PTR(-EINVAL);
  949. if (ctx_id != SVGA3D_INVALID_ID) {
  950. header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  951. header->cb_header->dxContext = ctx_id;
  952. }
  953. header->reserved = size;
  954. return header->cmd;
  955. }
  956. /**
  957. * vmw_cmdbuf_commit - Commit commands in a command buffer.
  958. *
  959. * @man: The command buffer manager.
  960. * @size: The size of the commands actually written.
  961. * @header: Header of the command buffer. NULL if the current command buffer
  962. * should be used.
  963. * @flush: Whether to flush the command buffer immediately.
  964. */
  965. void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
  966. struct vmw_cmdbuf_header *header, bool flush)
  967. {
  968. if (!header) {
  969. vmw_cmdbuf_commit_cur(man, size, flush);
  970. return;
  971. }
  972. (void) vmw_cmdbuf_cur_lock(man, false);
  973. __vmw_cmdbuf_cur_flush(man);
  974. WARN_ON(size > header->reserved);
  975. man->cur = header;
  976. man->cur_pos = size;
  977. if (!size)
  978. header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  979. if (flush)
  980. __vmw_cmdbuf_cur_flush(man);
  981. vmw_cmdbuf_cur_unlock(man);
  982. }
  983. /**
  984. * vmw_cmdbuf_send_device_command - Send a command through the device context.
  985. *
  986. * @man: The command buffer manager.
  987. * @command: Pointer to the command to send.
  988. * @size: Size of the command.
  989. *
  990. * Synchronously sends a device context command.
  991. */
  992. static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man,
  993. const void *command,
  994. size_t size)
  995. {
  996. struct vmw_cmdbuf_header *header;
  997. int status;
  998. void *cmd = vmw_cmdbuf_alloc(man, size, false, &header);
  999. if (IS_ERR(cmd))
  1000. return PTR_ERR(cmd);
  1001. memcpy(cmd, command, size);
  1002. header->cb_header->length = size;
  1003. header->cb_context = SVGA_CB_CONTEXT_DEVICE;
  1004. spin_lock(&man->lock);
  1005. status = vmw_cmdbuf_header_submit(header);
  1006. spin_unlock(&man->lock);
  1007. vmw_cmdbuf_header_free(header);
  1008. if (status != SVGA_CB_STATUS_COMPLETED) {
  1009. DRM_ERROR("Device context command failed with status %d\n",
  1010. status);
  1011. return -EINVAL;
  1012. }
  1013. return 0;
  1014. }
  1015. /**
  1016. * vmw_cmdbuf_preempt - Send a preempt command through the device
  1017. * context.
  1018. *
  1019. * @man: The command buffer manager.
  1020. *
  1021. * Synchronously sends a preempt command.
  1022. */
  1023. static int vmw_cmdbuf_preempt(struct vmw_cmdbuf_man *man, u32 context)
  1024. {
  1025. struct {
  1026. uint32 id;
  1027. SVGADCCmdPreempt body;
  1028. } __packed cmd;
  1029. cmd.id = SVGA_DC_CMD_PREEMPT;
  1030. cmd.body.context = SVGA_CB_CONTEXT_0 + context;
  1031. cmd.body.ignoreIDZero = 0;
  1032. return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
  1033. }
  1034. /**
  1035. * vmw_cmdbuf_startstop - Send a start / stop command through the device
  1036. * context.
  1037. *
  1038. * @man: The command buffer manager.
  1039. * @enable: Whether to enable or disable the context.
  1040. *
  1041. * Synchronously sends a device start / stop context command.
  1042. */
  1043. static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, u32 context,
  1044. bool enable)
  1045. {
  1046. struct {
  1047. uint32 id;
  1048. SVGADCCmdStartStop body;
  1049. } __packed cmd;
  1050. cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT;
  1051. cmd.body.enable = (enable) ? 1 : 0;
  1052. cmd.body.context = SVGA_CB_CONTEXT_0 + context;
  1053. return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
  1054. }
  1055. /**
  1056. * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes
  1057. *
  1058. * @man: The command buffer manager.
  1059. * @size: The size of the main space pool.
  1060. * @default_size: The default size of the command buffer for small kernel
  1061. * submissions.
  1062. *
  1063. * Set the size and allocate the main command buffer space pool,
  1064. * as well as the default size of the command buffer for
  1065. * small kernel submissions. If successful, this enables large command
  1066. * submissions. Note that this function requires that rudimentary command
  1067. * submission is already available and that the MOB memory manager is alive.
  1068. * Returns 0 on success. Negative error code on failure.
  1069. */
  1070. int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man,
  1071. size_t size, size_t default_size)
  1072. {
  1073. struct vmw_private *dev_priv = man->dev_priv;
  1074. bool dummy;
  1075. int ret;
  1076. if (man->has_pool)
  1077. return -EINVAL;
  1078. /* First, try to allocate a huge chunk of DMA memory */
  1079. size = PAGE_ALIGN(size);
  1080. man->map = dma_alloc_coherent(&dev_priv->dev->pdev->dev, size,
  1081. &man->handle, GFP_KERNEL);
  1082. if (man->map) {
  1083. man->using_mob = false;
  1084. } else {
  1085. /*
  1086. * DMA memory failed. If we can have command buffers in a
  1087. * MOB, try to use that instead. Note that this will
  1088. * actually call into the already enabled manager, when
  1089. * binding the MOB.
  1090. */
  1091. if (!(dev_priv->capabilities & SVGA_CAP_DX))
  1092. return -ENOMEM;
  1093. ret = ttm_bo_create(&dev_priv->bdev, size, ttm_bo_type_device,
  1094. &vmw_mob_ne_placement, 0, false, NULL,
  1095. &man->cmd_space);
  1096. if (ret)
  1097. return ret;
  1098. man->using_mob = true;
  1099. ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT,
  1100. &man->map_obj);
  1101. if (ret)
  1102. goto out_no_map;
  1103. man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy);
  1104. }
  1105. man->size = size;
  1106. drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT);
  1107. man->has_pool = true;
  1108. /*
  1109. * For now, set the default size to VMW_CMDBUF_INLINE_SIZE to
  1110. * prevent deadlocks from happening when vmw_cmdbuf_space_pool()
  1111. * needs to wait for space and we block on further command
  1112. * submissions to be able to free up space.
  1113. */
  1114. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1115. DRM_INFO("Using command buffers with %s pool.\n",
  1116. (man->using_mob) ? "MOB" : "DMA");
  1117. return 0;
  1118. out_no_map:
  1119. if (man->using_mob)
  1120. ttm_bo_unref(&man->cmd_space);
  1121. return ret;
  1122. }
  1123. /**
  1124. * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for
  1125. * inline command buffer submissions only.
  1126. *
  1127. * @dev_priv: Pointer to device private structure.
  1128. *
  1129. * Returns a pointer to a cummand buffer manager to success or error pointer
  1130. * on failure. The command buffer manager will be enabled for submissions of
  1131. * size VMW_CMDBUF_INLINE_SIZE only.
  1132. */
  1133. struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv)
  1134. {
  1135. struct vmw_cmdbuf_man *man;
  1136. struct vmw_cmdbuf_context *ctx;
  1137. unsigned int i;
  1138. int ret;
  1139. if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS))
  1140. return ERR_PTR(-ENOSYS);
  1141. man = kzalloc(sizeof(*man), GFP_KERNEL);
  1142. if (!man)
  1143. return ERR_PTR(-ENOMEM);
  1144. man->headers = dma_pool_create("vmwgfx cmdbuf",
  1145. &dev_priv->dev->pdev->dev,
  1146. sizeof(SVGACBHeader),
  1147. 64, PAGE_SIZE);
  1148. if (!man->headers) {
  1149. ret = -ENOMEM;
  1150. goto out_no_pool;
  1151. }
  1152. man->dheaders = dma_pool_create("vmwgfx inline cmdbuf",
  1153. &dev_priv->dev->pdev->dev,
  1154. sizeof(struct vmw_cmdbuf_dheader),
  1155. 64, PAGE_SIZE);
  1156. if (!man->dheaders) {
  1157. ret = -ENOMEM;
  1158. goto out_no_dpool;
  1159. }
  1160. for_each_cmdbuf_ctx(man, i, ctx)
  1161. vmw_cmdbuf_ctx_init(ctx);
  1162. INIT_LIST_HEAD(&man->error);
  1163. spin_lock_init(&man->lock);
  1164. mutex_init(&man->cur_mutex);
  1165. mutex_init(&man->space_mutex);
  1166. mutex_init(&man->error_mutex);
  1167. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1168. init_waitqueue_head(&man->alloc_queue);
  1169. init_waitqueue_head(&man->idle_queue);
  1170. man->dev_priv = dev_priv;
  1171. man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1;
  1172. INIT_WORK(&man->work, &vmw_cmdbuf_work_func);
  1173. vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR,
  1174. &dev_priv->error_waiters);
  1175. for_each_cmdbuf_ctx(man, i, ctx) {
  1176. ret = vmw_cmdbuf_startstop(man, i, true);
  1177. if (ret) {
  1178. DRM_ERROR("Failed starting command buffer "
  1179. "context %u.\n", i);
  1180. vmw_cmdbuf_man_destroy(man);
  1181. return ERR_PTR(ret);
  1182. }
  1183. }
  1184. return man;
  1185. out_no_dpool:
  1186. dma_pool_destroy(man->headers);
  1187. out_no_pool:
  1188. kfree(man);
  1189. return ERR_PTR(ret);
  1190. }
  1191. /**
  1192. * vmw_cmdbuf_remove_pool - Take down the main buffer space pool.
  1193. *
  1194. * @man: Pointer to a command buffer manager.
  1195. *
  1196. * This function removes the main buffer space pool, and should be called
  1197. * before MOB memory management is removed. When this function has been called,
  1198. * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or
  1199. * less are allowed, and the default size of the command buffer for small kernel
  1200. * submissions is also set to this size.
  1201. */
  1202. void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man)
  1203. {
  1204. if (!man->has_pool)
  1205. return;
  1206. man->has_pool = false;
  1207. man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1208. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1209. if (man->using_mob) {
  1210. (void) ttm_bo_kunmap(&man->map_obj);
  1211. ttm_bo_unref(&man->cmd_space);
  1212. } else {
  1213. dma_free_coherent(&man->dev_priv->dev->pdev->dev,
  1214. man->size, man->map, man->handle);
  1215. }
  1216. }
  1217. /**
  1218. * vmw_cmdbuf_man_destroy - Take down a command buffer manager.
  1219. *
  1220. * @man: Pointer to a command buffer manager.
  1221. *
  1222. * This function idles and then destroys a command buffer manager.
  1223. */
  1224. void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man)
  1225. {
  1226. struct vmw_cmdbuf_context *ctx;
  1227. unsigned int i;
  1228. WARN_ON_ONCE(man->has_pool);
  1229. (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1230. for_each_cmdbuf_ctx(man, i, ctx)
  1231. if (vmw_cmdbuf_startstop(man, i, false))
  1232. DRM_ERROR("Failed stopping command buffer "
  1233. "context %u.\n", i);
  1234. vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR,
  1235. &man->dev_priv->error_waiters);
  1236. (void) cancel_work_sync(&man->work);
  1237. dma_pool_destroy(man->dheaders);
  1238. dma_pool_destroy(man->headers);
  1239. mutex_destroy(&man->cur_mutex);
  1240. mutex_destroy(&man->space_mutex);
  1241. mutex_destroy(&man->error_mutex);
  1242. kfree(man);
  1243. }