i915_gem_context.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright © 2017 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include "../i915_selftest.h"
  25. #include "mock_drm.h"
  26. #include "huge_gem_object.h"
  27. #define DW_PER_PAGE (PAGE_SIZE / sizeof(u32))
  28. static struct i915_vma *
  29. gpu_fill_dw(struct i915_vma *vma, u64 offset, unsigned long count, u32 value)
  30. {
  31. struct drm_i915_gem_object *obj;
  32. const int gen = INTEL_GEN(vma->vm->i915);
  33. unsigned long n, size;
  34. u32 *cmd;
  35. int err;
  36. GEM_BUG_ON(!igt_can_mi_store_dword_imm(vma->vm->i915));
  37. size = (4 * count + 1) * sizeof(u32);
  38. size = round_up(size, PAGE_SIZE);
  39. obj = i915_gem_object_create_internal(vma->vm->i915, size);
  40. if (IS_ERR(obj))
  41. return ERR_CAST(obj);
  42. cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
  43. if (IS_ERR(cmd)) {
  44. err = PTR_ERR(cmd);
  45. goto err;
  46. }
  47. GEM_BUG_ON(offset + (count - 1) * PAGE_SIZE > vma->node.size);
  48. offset += vma->node.start;
  49. for (n = 0; n < count; n++) {
  50. if (gen >= 8) {
  51. *cmd++ = MI_STORE_DWORD_IMM_GEN4;
  52. *cmd++ = lower_32_bits(offset);
  53. *cmd++ = upper_32_bits(offset);
  54. *cmd++ = value;
  55. } else if (gen >= 4) {
  56. *cmd++ = MI_STORE_DWORD_IMM_GEN4 |
  57. (gen < 6 ? 1 << 22 : 0);
  58. *cmd++ = 0;
  59. *cmd++ = offset;
  60. *cmd++ = value;
  61. } else {
  62. *cmd++ = MI_STORE_DWORD_IMM | 1 << 22;
  63. *cmd++ = offset;
  64. *cmd++ = value;
  65. }
  66. offset += PAGE_SIZE;
  67. }
  68. *cmd = MI_BATCH_BUFFER_END;
  69. i915_gem_object_unpin_map(obj);
  70. err = i915_gem_object_set_to_gtt_domain(obj, false);
  71. if (err)
  72. goto err;
  73. vma = i915_vma_instance(obj, vma->vm, NULL);
  74. if (IS_ERR(vma)) {
  75. err = PTR_ERR(vma);
  76. goto err;
  77. }
  78. err = i915_vma_pin(vma, 0, 0, PIN_USER);
  79. if (err)
  80. goto err;
  81. return vma;
  82. err:
  83. i915_gem_object_put(obj);
  84. return ERR_PTR(err);
  85. }
  86. static unsigned long real_page_count(struct drm_i915_gem_object *obj)
  87. {
  88. return huge_gem_object_phys_size(obj) >> PAGE_SHIFT;
  89. }
  90. static unsigned long fake_page_count(struct drm_i915_gem_object *obj)
  91. {
  92. return huge_gem_object_dma_size(obj) >> PAGE_SHIFT;
  93. }
  94. static int gpu_fill(struct drm_i915_gem_object *obj,
  95. struct i915_gem_context *ctx,
  96. struct intel_engine_cs *engine,
  97. unsigned int dw)
  98. {
  99. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  100. struct i915_address_space *vm =
  101. ctx->ppgtt ? &ctx->ppgtt->base : &i915->ggtt.base;
  102. struct drm_i915_gem_request *rq;
  103. struct i915_vma *vma;
  104. struct i915_vma *batch;
  105. unsigned int flags;
  106. int err;
  107. GEM_BUG_ON(obj->base.size > vm->total);
  108. vma = i915_vma_instance(obj, vm, NULL);
  109. if (IS_ERR(vma))
  110. return PTR_ERR(vma);
  111. err = i915_gem_object_set_to_gtt_domain(obj, false);
  112. if (err)
  113. return err;
  114. err = i915_vma_pin(vma, 0, 0, PIN_HIGH | PIN_USER);
  115. if (err)
  116. return err;
  117. /* Within the GTT the huge objects maps every page onto
  118. * its 1024 real pages (using phys_pfn = dma_pfn % 1024).
  119. * We set the nth dword within the page using the nth
  120. * mapping via the GTT - this should exercise the GTT mapping
  121. * whilst checking that each context provides a unique view
  122. * into the object.
  123. */
  124. batch = gpu_fill_dw(vma,
  125. (dw * real_page_count(obj)) << PAGE_SHIFT |
  126. (dw * sizeof(u32)),
  127. real_page_count(obj),
  128. dw);
  129. if (IS_ERR(batch)) {
  130. err = PTR_ERR(batch);
  131. goto err_vma;
  132. }
  133. rq = i915_gem_request_alloc(engine, ctx);
  134. if (IS_ERR(rq)) {
  135. err = PTR_ERR(rq);
  136. goto err_batch;
  137. }
  138. err = engine->emit_flush(rq, EMIT_INVALIDATE);
  139. if (err)
  140. goto err_request;
  141. err = i915_switch_context(rq);
  142. if (err)
  143. goto err_request;
  144. flags = 0;
  145. if (INTEL_GEN(vm->i915) <= 5)
  146. flags |= I915_DISPATCH_SECURE;
  147. err = engine->emit_bb_start(rq,
  148. batch->node.start, batch->node.size,
  149. flags);
  150. if (err)
  151. goto err_request;
  152. i915_vma_move_to_active(batch, rq, 0);
  153. i915_gem_object_set_active_reference(batch->obj);
  154. i915_vma_unpin(batch);
  155. i915_vma_close(batch);
  156. i915_vma_move_to_active(vma, rq, 0);
  157. i915_vma_unpin(vma);
  158. reservation_object_lock(obj->resv, NULL);
  159. reservation_object_add_excl_fence(obj->resv, &rq->fence);
  160. reservation_object_unlock(obj->resv);
  161. __i915_add_request(rq, true);
  162. return 0;
  163. err_request:
  164. __i915_add_request(rq, false);
  165. err_batch:
  166. i915_vma_unpin(batch);
  167. err_vma:
  168. i915_vma_unpin(vma);
  169. return err;
  170. }
  171. static int cpu_fill(struct drm_i915_gem_object *obj, u32 value)
  172. {
  173. const bool has_llc = HAS_LLC(to_i915(obj->base.dev));
  174. unsigned int n, m, need_flush;
  175. int err;
  176. err = i915_gem_obj_prepare_shmem_write(obj, &need_flush);
  177. if (err)
  178. return err;
  179. for (n = 0; n < real_page_count(obj); n++) {
  180. u32 *map;
  181. map = kmap_atomic(i915_gem_object_get_page(obj, n));
  182. for (m = 0; m < DW_PER_PAGE; m++)
  183. map[m] = value;
  184. if (!has_llc)
  185. drm_clflush_virt_range(map, PAGE_SIZE);
  186. kunmap_atomic(map);
  187. }
  188. i915_gem_obj_finish_shmem_access(obj);
  189. obj->base.read_domains = I915_GEM_DOMAIN_GTT | I915_GEM_DOMAIN_CPU;
  190. obj->base.write_domain = 0;
  191. return 0;
  192. }
  193. static int cpu_check(struct drm_i915_gem_object *obj, unsigned int max)
  194. {
  195. unsigned int n, m, needs_flush;
  196. int err;
  197. err = i915_gem_obj_prepare_shmem_read(obj, &needs_flush);
  198. if (err)
  199. return err;
  200. for (n = 0; n < real_page_count(obj); n++) {
  201. u32 *map;
  202. map = kmap_atomic(i915_gem_object_get_page(obj, n));
  203. if (needs_flush & CLFLUSH_BEFORE)
  204. drm_clflush_virt_range(map, PAGE_SIZE);
  205. for (m = 0; m < max; m++) {
  206. if (map[m] != m) {
  207. pr_err("Invalid value at page %d, offset %d: found %x expected %x\n",
  208. n, m, map[m], m);
  209. err = -EINVAL;
  210. goto out_unmap;
  211. }
  212. }
  213. for (; m < DW_PER_PAGE; m++) {
  214. if (map[m] != 0xdeadbeef) {
  215. pr_err("Invalid value at page %d, offset %d: found %x expected %x\n",
  216. n, m, map[m], 0xdeadbeef);
  217. err = -EINVAL;
  218. goto out_unmap;
  219. }
  220. }
  221. out_unmap:
  222. kunmap_atomic(map);
  223. if (err)
  224. break;
  225. }
  226. i915_gem_obj_finish_shmem_access(obj);
  227. return err;
  228. }
  229. static struct drm_i915_gem_object *
  230. create_test_object(struct i915_gem_context *ctx,
  231. struct drm_file *file,
  232. struct list_head *objects)
  233. {
  234. struct drm_i915_gem_object *obj;
  235. struct i915_address_space *vm =
  236. ctx->ppgtt ? &ctx->ppgtt->base : &ctx->i915->ggtt.base;
  237. u64 size;
  238. u32 handle;
  239. int err;
  240. size = min(vm->total / 2, 1024ull * DW_PER_PAGE * PAGE_SIZE);
  241. size = round_down(size, DW_PER_PAGE * PAGE_SIZE);
  242. obj = huge_gem_object(ctx->i915, DW_PER_PAGE * PAGE_SIZE, size);
  243. if (IS_ERR(obj))
  244. return obj;
  245. /* tie the handle to the drm_file for easy reaping */
  246. err = drm_gem_handle_create(file, &obj->base, &handle);
  247. i915_gem_object_put(obj);
  248. if (err)
  249. return ERR_PTR(err);
  250. err = cpu_fill(obj, 0xdeadbeef);
  251. if (err) {
  252. pr_err("Failed to fill object with cpu, err=%d\n",
  253. err);
  254. return ERR_PTR(err);
  255. }
  256. list_add_tail(&obj->st_link, objects);
  257. return obj;
  258. }
  259. static unsigned long max_dwords(struct drm_i915_gem_object *obj)
  260. {
  261. unsigned long npages = fake_page_count(obj);
  262. GEM_BUG_ON(!IS_ALIGNED(npages, DW_PER_PAGE));
  263. return npages / DW_PER_PAGE;
  264. }
  265. static int igt_ctx_exec(void *arg)
  266. {
  267. struct drm_i915_private *i915 = arg;
  268. struct drm_i915_gem_object *obj = NULL;
  269. struct drm_file *file;
  270. IGT_TIMEOUT(end_time);
  271. LIST_HEAD(objects);
  272. unsigned long ncontexts, ndwords, dw;
  273. bool first_shared_gtt = true;
  274. int err;
  275. /* Create a few different contexts (with different mm) and write
  276. * through each ctx/mm using the GPU making sure those writes end
  277. * up in the expected pages of our obj.
  278. */
  279. file = mock_file(i915);
  280. if (IS_ERR(file))
  281. return PTR_ERR(file);
  282. mutex_lock(&i915->drm.struct_mutex);
  283. ncontexts = 0;
  284. ndwords = 0;
  285. dw = 0;
  286. while (!time_after(jiffies, end_time)) {
  287. struct intel_engine_cs *engine;
  288. struct i915_gem_context *ctx;
  289. unsigned int id;
  290. if (first_shared_gtt) {
  291. ctx = __create_hw_context(i915, file->driver_priv);
  292. first_shared_gtt = false;
  293. } else {
  294. ctx = i915_gem_create_context(i915, file->driver_priv);
  295. }
  296. if (IS_ERR(ctx)) {
  297. err = PTR_ERR(ctx);
  298. goto out_unlock;
  299. }
  300. for_each_engine(engine, i915, id) {
  301. if (!obj) {
  302. obj = create_test_object(ctx, file, &objects);
  303. if (IS_ERR(obj)) {
  304. err = PTR_ERR(obj);
  305. goto out_unlock;
  306. }
  307. }
  308. err = gpu_fill(obj, ctx, engine, dw);
  309. if (err) {
  310. pr_err("Failed to fill dword %lu [%lu/%lu] with gpu (%s) in ctx %u [full-ppgtt? %s], err=%d\n",
  311. ndwords, dw, max_dwords(obj),
  312. engine->name, ctx->hw_id,
  313. yesno(!!ctx->ppgtt), err);
  314. goto out_unlock;
  315. }
  316. if (++dw == max_dwords(obj)) {
  317. obj = NULL;
  318. dw = 0;
  319. }
  320. ndwords++;
  321. }
  322. ncontexts++;
  323. }
  324. pr_info("Submitted %lu contexts (across %u engines), filling %lu dwords\n",
  325. ncontexts, INTEL_INFO(i915)->num_rings, ndwords);
  326. dw = 0;
  327. list_for_each_entry(obj, &objects, st_link) {
  328. unsigned int rem =
  329. min_t(unsigned int, ndwords - dw, max_dwords(obj));
  330. err = cpu_check(obj, rem);
  331. if (err)
  332. break;
  333. dw += rem;
  334. }
  335. out_unlock:
  336. mutex_unlock(&i915->drm.struct_mutex);
  337. mock_file_free(i915, file);
  338. return err;
  339. }
  340. static int fake_aliasing_ppgtt_enable(struct drm_i915_private *i915)
  341. {
  342. struct drm_i915_gem_object *obj;
  343. int err;
  344. err = i915_gem_init_aliasing_ppgtt(i915);
  345. if (err)
  346. return err;
  347. list_for_each_entry(obj, &i915->mm.bound_list, global_link) {
  348. struct i915_vma *vma;
  349. vma = i915_vma_instance(obj, &i915->ggtt.base, NULL);
  350. if (IS_ERR(vma))
  351. continue;
  352. vma->flags &= ~I915_VMA_LOCAL_BIND;
  353. }
  354. return 0;
  355. }
  356. static void fake_aliasing_ppgtt_disable(struct drm_i915_private *i915)
  357. {
  358. i915_gem_fini_aliasing_ppgtt(i915);
  359. }
  360. int i915_gem_context_live_selftests(struct drm_i915_private *dev_priv)
  361. {
  362. static const struct i915_subtest tests[] = {
  363. SUBTEST(igt_ctx_exec),
  364. };
  365. bool fake_alias = false;
  366. int err;
  367. /* Install a fake aliasing gtt for exercise */
  368. if (USES_PPGTT(dev_priv) && !dev_priv->mm.aliasing_ppgtt) {
  369. mutex_lock(&dev_priv->drm.struct_mutex);
  370. err = fake_aliasing_ppgtt_enable(dev_priv);
  371. mutex_unlock(&dev_priv->drm.struct_mutex);
  372. if (err)
  373. return err;
  374. GEM_BUG_ON(!dev_priv->mm.aliasing_ppgtt);
  375. fake_alias = true;
  376. }
  377. err = i915_subtests(tests, dev_priv);
  378. if (fake_alias) {
  379. mutex_lock(&dev_priv->drm.struct_mutex);
  380. fake_aliasing_ppgtt_disable(dev_priv);
  381. mutex_unlock(&dev_priv->drm.struct_mutex);
  382. }
  383. return err;
  384. }