vmwgfx_cmdbuf.c 39 KB

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