vmwgfx_cmdbuf.c 39 KB

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