i915_gem_context.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 "igt_flush_test.h"
  26. #include "mock_drm.h"
  27. #include "huge_gem_object.h"
  28. #define DW_PER_PAGE (PAGE_SIZE / sizeof(u32))
  29. static struct i915_vma *
  30. gpu_fill_dw(struct i915_vma *vma, u64 offset, unsigned long count, u32 value)
  31. {
  32. struct drm_i915_gem_object *obj;
  33. const int gen = INTEL_GEN(vma->vm->i915);
  34. unsigned long n, size;
  35. u32 *cmd;
  36. int err;
  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 i915_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. GEM_BUG_ON(!intel_engine_can_store_dword(engine));
  109. vma = i915_vma_instance(obj, vm, NULL);
  110. if (IS_ERR(vma))
  111. return PTR_ERR(vma);
  112. err = i915_gem_object_set_to_gtt_domain(obj, false);
  113. if (err)
  114. return err;
  115. err = i915_vma_pin(vma, 0, 0, PIN_HIGH | PIN_USER);
  116. if (err)
  117. return err;
  118. /* Within the GTT the huge objects maps every page onto
  119. * its 1024 real pages (using phys_pfn = dma_pfn % 1024).
  120. * We set the nth dword within the page using the nth
  121. * mapping via the GTT - this should exercise the GTT mapping
  122. * whilst checking that each context provides a unique view
  123. * into the object.
  124. */
  125. batch = gpu_fill_dw(vma,
  126. (dw * real_page_count(obj)) << PAGE_SHIFT |
  127. (dw * sizeof(u32)),
  128. real_page_count(obj),
  129. dw);
  130. if (IS_ERR(batch)) {
  131. err = PTR_ERR(batch);
  132. goto err_vma;
  133. }
  134. rq = i915_request_alloc(engine, ctx);
  135. if (IS_ERR(rq)) {
  136. err = PTR_ERR(rq);
  137. goto err_batch;
  138. }
  139. flags = 0;
  140. if (INTEL_GEN(vm->i915) <= 5)
  141. flags |= I915_DISPATCH_SECURE;
  142. err = engine->emit_bb_start(rq,
  143. batch->node.start, batch->node.size,
  144. flags);
  145. if (err)
  146. goto err_request;
  147. i915_vma_move_to_active(batch, rq, 0);
  148. i915_gem_object_set_active_reference(batch->obj);
  149. i915_vma_unpin(batch);
  150. i915_vma_close(batch);
  151. i915_vma_move_to_active(vma, rq, 0);
  152. i915_vma_unpin(vma);
  153. reservation_object_lock(obj->resv, NULL);
  154. reservation_object_add_excl_fence(obj->resv, &rq->fence);
  155. reservation_object_unlock(obj->resv);
  156. __i915_request_add(rq, true);
  157. return 0;
  158. err_request:
  159. __i915_request_add(rq, false);
  160. err_batch:
  161. i915_vma_unpin(batch);
  162. err_vma:
  163. i915_vma_unpin(vma);
  164. return err;
  165. }
  166. static int cpu_fill(struct drm_i915_gem_object *obj, u32 value)
  167. {
  168. const bool has_llc = HAS_LLC(to_i915(obj->base.dev));
  169. unsigned int n, m, need_flush;
  170. int err;
  171. err = i915_gem_obj_prepare_shmem_write(obj, &need_flush);
  172. if (err)
  173. return err;
  174. for (n = 0; n < real_page_count(obj); n++) {
  175. u32 *map;
  176. map = kmap_atomic(i915_gem_object_get_page(obj, n));
  177. for (m = 0; m < DW_PER_PAGE; m++)
  178. map[m] = value;
  179. if (!has_llc)
  180. drm_clflush_virt_range(map, PAGE_SIZE);
  181. kunmap_atomic(map);
  182. }
  183. i915_gem_obj_finish_shmem_access(obj);
  184. obj->read_domains = I915_GEM_DOMAIN_GTT | I915_GEM_DOMAIN_CPU;
  185. obj->write_domain = 0;
  186. return 0;
  187. }
  188. static int cpu_check(struct drm_i915_gem_object *obj, unsigned int max)
  189. {
  190. unsigned int n, m, needs_flush;
  191. int err;
  192. err = i915_gem_obj_prepare_shmem_read(obj, &needs_flush);
  193. if (err)
  194. return err;
  195. for (n = 0; n < real_page_count(obj); n++) {
  196. u32 *map;
  197. map = kmap_atomic(i915_gem_object_get_page(obj, n));
  198. if (needs_flush & CLFLUSH_BEFORE)
  199. drm_clflush_virt_range(map, PAGE_SIZE);
  200. for (m = 0; m < max; m++) {
  201. if (map[m] != m) {
  202. pr_err("Invalid value at page %d, offset %d: found %x expected %x\n",
  203. n, m, map[m], m);
  204. err = -EINVAL;
  205. goto out_unmap;
  206. }
  207. }
  208. for (; m < DW_PER_PAGE; m++) {
  209. if (map[m] != 0xdeadbeef) {
  210. pr_err("Invalid value at page %d, offset %d: found %x expected %x\n",
  211. n, m, map[m], 0xdeadbeef);
  212. err = -EINVAL;
  213. goto out_unmap;
  214. }
  215. }
  216. out_unmap:
  217. kunmap_atomic(map);
  218. if (err)
  219. break;
  220. }
  221. i915_gem_obj_finish_shmem_access(obj);
  222. return err;
  223. }
  224. static int file_add_object(struct drm_file *file,
  225. struct drm_i915_gem_object *obj)
  226. {
  227. int err;
  228. GEM_BUG_ON(obj->base.handle_count);
  229. /* tie the object to the drm_file for easy reaping */
  230. err = idr_alloc(&file->object_idr, &obj->base, 1, 0, GFP_KERNEL);
  231. if (err < 0)
  232. return err;
  233. i915_gem_object_get(obj);
  234. obj->base.handle_count++;
  235. return 0;
  236. }
  237. static struct drm_i915_gem_object *
  238. create_test_object(struct i915_gem_context *ctx,
  239. struct drm_file *file,
  240. struct list_head *objects)
  241. {
  242. struct drm_i915_gem_object *obj;
  243. struct i915_address_space *vm =
  244. ctx->ppgtt ? &ctx->ppgtt->base : &ctx->i915->ggtt.base;
  245. u64 size;
  246. int err;
  247. size = min(vm->total / 2, 1024ull * DW_PER_PAGE * PAGE_SIZE);
  248. size = round_down(size, DW_PER_PAGE * PAGE_SIZE);
  249. obj = huge_gem_object(ctx->i915, DW_PER_PAGE * PAGE_SIZE, size);
  250. if (IS_ERR(obj))
  251. return obj;
  252. err = file_add_object(file, obj);
  253. i915_gem_object_put(obj);
  254. if (err)
  255. return ERR_PTR(err);
  256. err = cpu_fill(obj, 0xdeadbeef);
  257. if (err) {
  258. pr_err("Failed to fill object with cpu, err=%d\n",
  259. err);
  260. return ERR_PTR(err);
  261. }
  262. list_add_tail(&obj->st_link, objects);
  263. return obj;
  264. }
  265. static unsigned long max_dwords(struct drm_i915_gem_object *obj)
  266. {
  267. unsigned long npages = fake_page_count(obj);
  268. GEM_BUG_ON(!IS_ALIGNED(npages, DW_PER_PAGE));
  269. return npages / DW_PER_PAGE;
  270. }
  271. static int igt_ctx_exec(void *arg)
  272. {
  273. struct drm_i915_private *i915 = arg;
  274. struct drm_i915_gem_object *obj = NULL;
  275. struct drm_file *file;
  276. IGT_TIMEOUT(end_time);
  277. LIST_HEAD(objects);
  278. unsigned long ncontexts, ndwords, dw;
  279. bool first_shared_gtt = true;
  280. int err = -ENODEV;
  281. /* Create a few different contexts (with different mm) and write
  282. * through each ctx/mm using the GPU making sure those writes end
  283. * up in the expected pages of our obj.
  284. */
  285. file = mock_file(i915);
  286. if (IS_ERR(file))
  287. return PTR_ERR(file);
  288. mutex_lock(&i915->drm.struct_mutex);
  289. ncontexts = 0;
  290. ndwords = 0;
  291. dw = 0;
  292. while (!time_after(jiffies, end_time)) {
  293. struct intel_engine_cs *engine;
  294. struct i915_gem_context *ctx;
  295. unsigned int id;
  296. if (first_shared_gtt) {
  297. ctx = __create_hw_context(i915, file->driver_priv);
  298. first_shared_gtt = false;
  299. } else {
  300. ctx = i915_gem_create_context(i915, file->driver_priv);
  301. }
  302. if (IS_ERR(ctx)) {
  303. err = PTR_ERR(ctx);
  304. goto out_unlock;
  305. }
  306. for_each_engine(engine, i915, id) {
  307. if (!intel_engine_can_store_dword(engine))
  308. continue;
  309. if (!obj) {
  310. obj = create_test_object(ctx, file, &objects);
  311. if (IS_ERR(obj)) {
  312. err = PTR_ERR(obj);
  313. goto out_unlock;
  314. }
  315. }
  316. intel_runtime_pm_get(i915);
  317. err = gpu_fill(obj, ctx, engine, dw);
  318. intel_runtime_pm_put(i915);
  319. if (err) {
  320. pr_err("Failed to fill dword %lu [%lu/%lu] with gpu (%s) in ctx %u [full-ppgtt? %s], err=%d\n",
  321. ndwords, dw, max_dwords(obj),
  322. engine->name, ctx->hw_id,
  323. yesno(!!ctx->ppgtt), err);
  324. goto out_unlock;
  325. }
  326. if (++dw == max_dwords(obj)) {
  327. obj = NULL;
  328. dw = 0;
  329. }
  330. ndwords++;
  331. }
  332. ncontexts++;
  333. }
  334. pr_info("Submitted %lu contexts (across %u engines), filling %lu dwords\n",
  335. ncontexts, INTEL_INFO(i915)->num_rings, ndwords);
  336. dw = 0;
  337. list_for_each_entry(obj, &objects, st_link) {
  338. unsigned int rem =
  339. min_t(unsigned int, ndwords - dw, max_dwords(obj));
  340. err = cpu_check(obj, rem);
  341. if (err)
  342. break;
  343. dw += rem;
  344. }
  345. out_unlock:
  346. if (igt_flush_test(i915, I915_WAIT_LOCKED))
  347. err = -EIO;
  348. mutex_unlock(&i915->drm.struct_mutex);
  349. mock_file_free(i915, file);
  350. return err;
  351. }
  352. static int fake_aliasing_ppgtt_enable(struct drm_i915_private *i915)
  353. {
  354. struct drm_i915_gem_object *obj;
  355. int err;
  356. err = i915_gem_init_aliasing_ppgtt(i915);
  357. if (err)
  358. return err;
  359. list_for_each_entry(obj, &i915->mm.bound_list, mm.link) {
  360. struct i915_vma *vma;
  361. vma = i915_vma_instance(obj, &i915->ggtt.base, NULL);
  362. if (IS_ERR(vma))
  363. continue;
  364. vma->flags &= ~I915_VMA_LOCAL_BIND;
  365. }
  366. return 0;
  367. }
  368. static void fake_aliasing_ppgtt_disable(struct drm_i915_private *i915)
  369. {
  370. i915_gem_fini_aliasing_ppgtt(i915);
  371. }
  372. int i915_gem_context_live_selftests(struct drm_i915_private *dev_priv)
  373. {
  374. static const struct i915_subtest tests[] = {
  375. SUBTEST(igt_ctx_exec),
  376. };
  377. bool fake_alias = false;
  378. int err;
  379. /* Install a fake aliasing gtt for exercise */
  380. if (USES_PPGTT(dev_priv) && !dev_priv->mm.aliasing_ppgtt) {
  381. mutex_lock(&dev_priv->drm.struct_mutex);
  382. err = fake_aliasing_ppgtt_enable(dev_priv);
  383. mutex_unlock(&dev_priv->drm.struct_mutex);
  384. if (err)
  385. return err;
  386. GEM_BUG_ON(!dev_priv->mm.aliasing_ppgtt);
  387. fake_alias = true;
  388. }
  389. err = i915_subtests(tests, dev_priv);
  390. if (fake_alias) {
  391. mutex_lock(&dev_priv->drm.struct_mutex);
  392. fake_aliasing_ppgtt_disable(dev_priv);
  393. mutex_unlock(&dev_priv->drm.struct_mutex);
  394. }
  395. return err;
  396. }