job.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Tegra host1x Job
  3. *
  4. * Copyright (c) 2010-2013, NVIDIA Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/dma-mapping.h>
  19. #include <linux/err.h>
  20. #include <linux/kref.h>
  21. #include <linux/module.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <trace/events/host1x.h>
  26. #include "channel.h"
  27. #include "dev.h"
  28. #include "host1x_bo.h"
  29. #include "job.h"
  30. #include "syncpt.h"
  31. struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
  32. u32 num_cmdbufs, u32 num_relocs,
  33. u32 num_waitchks)
  34. {
  35. struct host1x_job *job = NULL;
  36. unsigned int num_unpins = num_cmdbufs + num_relocs;
  37. u64 total;
  38. void *mem;
  39. /* Check that we're not going to overflow */
  40. total = sizeof(struct host1x_job) +
  41. num_relocs * sizeof(struct host1x_reloc) +
  42. num_unpins * sizeof(struct host1x_job_unpin_data) +
  43. num_waitchks * sizeof(struct host1x_waitchk) +
  44. num_cmdbufs * sizeof(struct host1x_job_gather) +
  45. num_unpins * sizeof(dma_addr_t) +
  46. num_unpins * sizeof(u32 *);
  47. if (total > ULONG_MAX)
  48. return NULL;
  49. mem = job = kzalloc(total, GFP_KERNEL);
  50. if (!job)
  51. return NULL;
  52. kref_init(&job->ref);
  53. job->channel = ch;
  54. /* Redistribute memory to the structs */
  55. mem += sizeof(struct host1x_job);
  56. job->relocarray = num_relocs ? mem : NULL;
  57. mem += num_relocs * sizeof(struct host1x_reloc);
  58. job->unpins = num_unpins ? mem : NULL;
  59. mem += num_unpins * sizeof(struct host1x_job_unpin_data);
  60. job->waitchk = num_waitchks ? mem : NULL;
  61. mem += num_waitchks * sizeof(struct host1x_waitchk);
  62. job->gathers = num_cmdbufs ? mem : NULL;
  63. mem += num_cmdbufs * sizeof(struct host1x_job_gather);
  64. job->addr_phys = num_unpins ? mem : NULL;
  65. job->reloc_addr_phys = job->addr_phys;
  66. job->gather_addr_phys = &job->addr_phys[num_relocs];
  67. return job;
  68. }
  69. struct host1x_job *host1x_job_get(struct host1x_job *job)
  70. {
  71. kref_get(&job->ref);
  72. return job;
  73. }
  74. static void job_free(struct kref *ref)
  75. {
  76. struct host1x_job *job = container_of(ref, struct host1x_job, ref);
  77. kfree(job);
  78. }
  79. void host1x_job_put(struct host1x_job *job)
  80. {
  81. kref_put(&job->ref, job_free);
  82. }
  83. void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
  84. u32 words, u32 offset)
  85. {
  86. struct host1x_job_gather *cur_gather = &job->gathers[job->num_gathers];
  87. cur_gather->words = words;
  88. cur_gather->bo = bo;
  89. cur_gather->offset = offset;
  90. job->num_gathers++;
  91. }
  92. /*
  93. * NULL an already satisfied WAIT_SYNCPT host method, by patching its
  94. * args in the command stream. The method data is changed to reference
  95. * a reserved (never given out or incr) HOST1X_SYNCPT_RESERVED syncpt
  96. * with a matching threshold value of 0, so is guaranteed to be popped
  97. * by the host HW.
  98. */
  99. static void host1x_syncpt_patch_offset(struct host1x_syncpt *sp,
  100. struct host1x_bo *h, u32 offset)
  101. {
  102. void *patch_addr = NULL;
  103. /* patch the wait */
  104. patch_addr = host1x_bo_kmap(h, offset >> PAGE_SHIFT);
  105. if (patch_addr) {
  106. host1x_syncpt_patch_wait(sp,
  107. patch_addr + (offset & ~PAGE_MASK));
  108. host1x_bo_kunmap(h, offset >> PAGE_SHIFT, patch_addr);
  109. } else
  110. pr_err("Could not map cmdbuf for wait check\n");
  111. }
  112. /*
  113. * Check driver supplied waitchk structs for syncpt thresholds
  114. * that have already been satisfied and NULL the comparison (to
  115. * avoid a wrap condition in the HW).
  116. */
  117. static int do_waitchks(struct host1x_job *job, struct host1x *host,
  118. struct host1x_bo *patch)
  119. {
  120. int i;
  121. /* compare syncpt vs wait threshold */
  122. for (i = 0; i < job->num_waitchk; i++) {
  123. struct host1x_waitchk *wait = &job->waitchk[i];
  124. struct host1x_syncpt *sp =
  125. host1x_syncpt_get(host, wait->syncpt_id);
  126. /* validate syncpt id */
  127. if (wait->syncpt_id > host1x_syncpt_nb_pts(host))
  128. continue;
  129. /* skip all other gathers */
  130. if (patch != wait->bo)
  131. continue;
  132. trace_host1x_syncpt_wait_check(wait->bo, wait->offset,
  133. wait->syncpt_id, wait->thresh,
  134. host1x_syncpt_read_min(sp));
  135. if (host1x_syncpt_is_expired(sp, wait->thresh)) {
  136. dev_dbg(host->dev,
  137. "drop WAIT id %d (%s) thresh 0x%x, min 0x%x\n",
  138. wait->syncpt_id, sp->name, wait->thresh,
  139. host1x_syncpt_read_min(sp));
  140. host1x_syncpt_patch_offset(sp, patch, wait->offset);
  141. }
  142. wait->bo = NULL;
  143. }
  144. return 0;
  145. }
  146. static unsigned int pin_job(struct host1x_job *job)
  147. {
  148. unsigned int i;
  149. job->num_unpins = 0;
  150. for (i = 0; i < job->num_relocs; i++) {
  151. struct host1x_reloc *reloc = &job->relocarray[i];
  152. struct sg_table *sgt;
  153. dma_addr_t phys_addr;
  154. reloc->target = host1x_bo_get(reloc->target);
  155. if (!reloc->target)
  156. goto unpin;
  157. phys_addr = host1x_bo_pin(reloc->target, &sgt);
  158. if (!phys_addr)
  159. goto unpin;
  160. job->addr_phys[job->num_unpins] = phys_addr;
  161. job->unpins[job->num_unpins].bo = reloc->target;
  162. job->unpins[job->num_unpins].sgt = sgt;
  163. job->num_unpins++;
  164. }
  165. for (i = 0; i < job->num_gathers; i++) {
  166. struct host1x_job_gather *g = &job->gathers[i];
  167. struct sg_table *sgt;
  168. dma_addr_t phys_addr;
  169. g->bo = host1x_bo_get(g->bo);
  170. if (!g->bo)
  171. goto unpin;
  172. phys_addr = host1x_bo_pin(g->bo, &sgt);
  173. if (!phys_addr)
  174. goto unpin;
  175. job->addr_phys[job->num_unpins] = phys_addr;
  176. job->unpins[job->num_unpins].bo = g->bo;
  177. job->unpins[job->num_unpins].sgt = sgt;
  178. job->num_unpins++;
  179. }
  180. return job->num_unpins;
  181. unpin:
  182. host1x_job_unpin(job);
  183. return 0;
  184. }
  185. static unsigned int do_relocs(struct host1x_job *job, struct host1x_bo *cmdbuf)
  186. {
  187. int i = 0;
  188. u32 last_page = ~0;
  189. void *cmdbuf_page_addr = NULL;
  190. /* pin & patch the relocs for one gather */
  191. for (i = 0; i < job->num_relocs; i++) {
  192. struct host1x_reloc *reloc = &job->relocarray[i];
  193. u32 reloc_addr = (job->reloc_addr_phys[i] +
  194. reloc->target_offset) >> reloc->shift;
  195. u32 *target;
  196. /* skip all other gathers */
  197. if (cmdbuf != reloc->cmdbuf)
  198. continue;
  199. if (last_page != reloc->cmdbuf_offset >> PAGE_SHIFT) {
  200. if (cmdbuf_page_addr)
  201. host1x_bo_kunmap(cmdbuf, last_page,
  202. cmdbuf_page_addr);
  203. cmdbuf_page_addr = host1x_bo_kmap(cmdbuf,
  204. reloc->cmdbuf_offset >> PAGE_SHIFT);
  205. last_page = reloc->cmdbuf_offset >> PAGE_SHIFT;
  206. if (unlikely(!cmdbuf_page_addr)) {
  207. pr_err("Could not map cmdbuf for relocation\n");
  208. return -ENOMEM;
  209. }
  210. }
  211. target = cmdbuf_page_addr + (reloc->cmdbuf_offset & ~PAGE_MASK);
  212. *target = reloc_addr;
  213. }
  214. if (cmdbuf_page_addr)
  215. host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr);
  216. return 0;
  217. }
  218. static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
  219. unsigned int offset)
  220. {
  221. offset *= sizeof(u32);
  222. if (reloc->cmdbuf != cmdbuf || reloc->cmdbuf_offset != offset)
  223. return false;
  224. return true;
  225. }
  226. struct host1x_firewall {
  227. struct host1x_job *job;
  228. struct device *dev;
  229. unsigned int num_relocs;
  230. struct host1x_reloc *reloc;
  231. struct host1x_bo *cmdbuf_id;
  232. unsigned int offset;
  233. u32 words;
  234. u32 class;
  235. u32 reg;
  236. u32 mask;
  237. u32 count;
  238. };
  239. static int check_mask(struct host1x_firewall *fw)
  240. {
  241. u32 mask = fw->mask;
  242. u32 reg = fw->reg;
  243. while (mask) {
  244. if (fw->words == 0)
  245. return -EINVAL;
  246. if (mask & 1) {
  247. if (fw->job->is_addr_reg(fw->dev, fw->class, reg)) {
  248. if (!fw->num_relocs)
  249. return -EINVAL;
  250. if (!check_reloc(fw->reloc, fw->cmdbuf_id,
  251. fw->offset))
  252. return -EINVAL;
  253. fw->reloc++;
  254. fw->num_relocs--;
  255. }
  256. fw->words--;
  257. fw->offset++;
  258. }
  259. mask >>= 1;
  260. reg++;
  261. }
  262. return 0;
  263. }
  264. static int check_incr(struct host1x_firewall *fw)
  265. {
  266. u32 count = fw->count;
  267. u32 reg = fw->reg;
  268. while (count) {
  269. if (fw->words == 0)
  270. return -EINVAL;
  271. if (fw->job->is_addr_reg(fw->dev, fw->class, reg)) {
  272. if (!fw->num_relocs)
  273. return -EINVAL;
  274. if (!check_reloc(fw->reloc, fw->cmdbuf_id, fw->offset))
  275. return -EINVAL;
  276. fw->reloc++;
  277. fw->num_relocs--;
  278. }
  279. reg++;
  280. fw->words--;
  281. fw->offset++;
  282. count--;
  283. }
  284. return 0;
  285. }
  286. static int check_nonincr(struct host1x_firewall *fw)
  287. {
  288. int is_addr_reg = fw->job->is_addr_reg(fw->dev, fw->class, fw->reg);
  289. u32 count = fw->count;
  290. while (count) {
  291. if (fw->words == 0)
  292. return -EINVAL;
  293. if (is_addr_reg) {
  294. if (!fw->num_relocs)
  295. return -EINVAL;
  296. if (!check_reloc(fw->reloc, fw->cmdbuf_id, fw->offset))
  297. return -EINVAL;
  298. fw->reloc++;
  299. fw->num_relocs--;
  300. }
  301. fw->words--;
  302. fw->offset++;
  303. count--;
  304. }
  305. return 0;
  306. }
  307. static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
  308. {
  309. u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
  310. (g->offset / sizeof(u32));
  311. int err = 0;
  312. if (!fw->job->is_addr_reg)
  313. return 0;
  314. fw->words = g->words;
  315. fw->cmdbuf_id = g->bo;
  316. fw->offset = 0;
  317. while (fw->words && !err) {
  318. u32 word = cmdbuf_base[fw->offset];
  319. u32 opcode = (word & 0xf0000000) >> 28;
  320. fw->mask = 0;
  321. fw->reg = 0;
  322. fw->count = 0;
  323. fw->words--;
  324. fw->offset++;
  325. switch (opcode) {
  326. case 0:
  327. fw->class = word >> 6 & 0x3ff;
  328. fw->mask = word & 0x3f;
  329. fw->reg = word >> 16 & 0xfff;
  330. err = check_mask(fw);
  331. if (err)
  332. goto out;
  333. break;
  334. case 1:
  335. fw->reg = word >> 16 & 0xfff;
  336. fw->count = word & 0xffff;
  337. err = check_incr(fw);
  338. if (err)
  339. goto out;
  340. break;
  341. case 2:
  342. fw->reg = word >> 16 & 0xfff;
  343. fw->count = word & 0xffff;
  344. err = check_nonincr(fw);
  345. if (err)
  346. goto out;
  347. break;
  348. case 3:
  349. fw->mask = word & 0xffff;
  350. fw->reg = word >> 16 & 0xfff;
  351. err = check_mask(fw);
  352. if (err)
  353. goto out;
  354. break;
  355. case 4:
  356. case 5:
  357. case 14:
  358. break;
  359. default:
  360. err = -EINVAL;
  361. break;
  362. }
  363. }
  364. /* No relocs should remain at this point */
  365. if (fw->num_relocs)
  366. err = -EINVAL;
  367. out:
  368. return err;
  369. }
  370. static inline int copy_gathers(struct host1x_job *job, struct device *dev)
  371. {
  372. struct host1x_firewall fw;
  373. size_t size = 0;
  374. size_t offset = 0;
  375. int i;
  376. fw.job = job;
  377. fw.dev = dev;
  378. fw.reloc = job->relocarray;
  379. fw.num_relocs = job->num_relocs;
  380. fw.class = 0;
  381. for (i = 0; i < job->num_gathers; i++) {
  382. struct host1x_job_gather *g = &job->gathers[i];
  383. size += g->words * sizeof(u32);
  384. }
  385. job->gather_copy_mapped = dma_alloc_writecombine(dev, size,
  386. &job->gather_copy,
  387. GFP_KERNEL);
  388. if (!job->gather_copy_mapped) {
  389. int err = PTR_ERR(job->gather_copy_mapped);
  390. job->gather_copy_mapped = NULL;
  391. return err;
  392. }
  393. job->gather_copy_size = size;
  394. for (i = 0; i < job->num_gathers; i++) {
  395. struct host1x_job_gather *g = &job->gathers[i];
  396. void *gather;
  397. /* Copy the gather */
  398. gather = host1x_bo_mmap(g->bo);
  399. memcpy(job->gather_copy_mapped + offset, gather + g->offset,
  400. g->words * sizeof(u32));
  401. host1x_bo_munmap(g->bo, gather);
  402. /* Store the location in the buffer */
  403. g->base = job->gather_copy;
  404. g->offset = offset;
  405. /* Validate the job */
  406. if (validate(&fw, g))
  407. return -EINVAL;
  408. offset += g->words * sizeof(u32);
  409. }
  410. return 0;
  411. }
  412. int host1x_job_pin(struct host1x_job *job, struct device *dev)
  413. {
  414. int err;
  415. unsigned int i, j;
  416. struct host1x *host = dev_get_drvdata(dev->parent);
  417. DECLARE_BITMAP(waitchk_mask, host1x_syncpt_nb_pts(host));
  418. bitmap_zero(waitchk_mask, host1x_syncpt_nb_pts(host));
  419. for (i = 0; i < job->num_waitchk; i++) {
  420. u32 syncpt_id = job->waitchk[i].syncpt_id;
  421. if (syncpt_id < host1x_syncpt_nb_pts(host))
  422. set_bit(syncpt_id, waitchk_mask);
  423. }
  424. /* get current syncpt values for waitchk */
  425. for_each_set_bit(i, waitchk_mask, host1x_syncpt_nb_pts(host))
  426. host1x_syncpt_load(host->syncpt + i);
  427. /* pin memory */
  428. err = pin_job(job);
  429. if (!err)
  430. goto out;
  431. /* patch gathers */
  432. for (i = 0; i < job->num_gathers; i++) {
  433. struct host1x_job_gather *g = &job->gathers[i];
  434. /* process each gather mem only once */
  435. if (g->handled)
  436. continue;
  437. g->base = job->gather_addr_phys[i];
  438. for (j = 0; j < job->num_gathers; j++)
  439. if (job->gathers[j].bo == g->bo)
  440. job->gathers[j].handled = true;
  441. err = do_relocs(job, g->bo);
  442. if (err)
  443. break;
  444. err = do_waitchks(job, host, g->bo);
  445. if (err)
  446. break;
  447. }
  448. if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !err) {
  449. err = copy_gathers(job, dev);
  450. if (err) {
  451. host1x_job_unpin(job);
  452. return err;
  453. }
  454. }
  455. out:
  456. wmb();
  457. return err;
  458. }
  459. void host1x_job_unpin(struct host1x_job *job)
  460. {
  461. unsigned int i;
  462. for (i = 0; i < job->num_unpins; i++) {
  463. struct host1x_job_unpin_data *unpin = &job->unpins[i];
  464. host1x_bo_unpin(unpin->bo, unpin->sgt);
  465. host1x_bo_put(unpin->bo);
  466. }
  467. job->num_unpins = 0;
  468. if (job->gather_copy_size)
  469. dma_free_writecombine(job->channel->dev, job->gather_copy_size,
  470. job->gather_copy_mapped,
  471. job->gather_copy);
  472. }
  473. /*
  474. * Debug routine used to dump job entries
  475. */
  476. void host1x_job_dump(struct device *dev, struct host1x_job *job)
  477. {
  478. dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id);
  479. dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
  480. dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
  481. dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
  482. dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
  483. dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);
  484. }