qdma.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * DMA driver for AMD Queue-based DMA Subsystem
  4. *
  5. * Copyright (C) 2023-2024, Advanced Micro Devices, Inc.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bitops.h>
  9. #include <linux/dmaengine.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/platform_data/amd_qdma.h>
  15. #include <linux/regmap.h>
  16. #include "qdma.h"
  17. #define CHAN_STR(q) (((q)->dir == DMA_MEM_TO_DEV) ? "H2C" : "C2H")
  18. #define QDMA_REG_OFF(d, r) ((d)->roffs[r].off)
  19. /* MMIO regmap config for all QDMA registers */
  20. static const struct regmap_config qdma_regmap_config = {
  21. .reg_bits = 32,
  22. .val_bits = 32,
  23. .reg_stride = 4,
  24. };
  25. static inline struct qdma_queue *to_qdma_queue(struct dma_chan *chan)
  26. {
  27. return container_of(chan, struct qdma_queue, vchan.chan);
  28. }
  29. static inline struct qdma_mm_vdesc *to_qdma_vdesc(struct virt_dma_desc *vdesc)
  30. {
  31. return container_of(vdesc, struct qdma_mm_vdesc, vdesc);
  32. }
  33. static inline u32 qdma_get_intr_ring_idx(struct qdma_device *qdev)
  34. {
  35. u32 idx;
  36. idx = qdev->qintr_rings[qdev->qintr_ring_idx++].ridx;
  37. qdev->qintr_ring_idx %= qdev->qintr_ring_num;
  38. return idx;
  39. }
  40. static u64 qdma_get_field(const struct qdma_device *qdev, const u32 *data,
  41. enum qdma_reg_fields field)
  42. {
  43. const struct qdma_reg_field *f = &qdev->rfields[field];
  44. u16 low_pos, hi_pos, low_bit, hi_bit;
  45. u64 value = 0, mask;
  46. low_pos = f->lsb / BITS_PER_TYPE(*data);
  47. hi_pos = f->msb / BITS_PER_TYPE(*data);
  48. if (low_pos == hi_pos) {
  49. low_bit = f->lsb % BITS_PER_TYPE(*data);
  50. hi_bit = f->msb % BITS_PER_TYPE(*data);
  51. mask = GENMASK(hi_bit, low_bit);
  52. value = (data[low_pos] & mask) >> low_bit;
  53. } else if (hi_pos == low_pos + 1) {
  54. low_bit = f->lsb % BITS_PER_TYPE(*data);
  55. hi_bit = low_bit + (f->msb - f->lsb);
  56. value = ((u64)data[hi_pos] << BITS_PER_TYPE(*data)) |
  57. data[low_pos];
  58. mask = GENMASK_ULL(hi_bit, low_bit);
  59. value = (value & mask) >> low_bit;
  60. } else {
  61. hi_bit = f->msb % BITS_PER_TYPE(*data);
  62. mask = GENMASK(hi_bit, 0);
  63. value = data[hi_pos] & mask;
  64. low_bit = f->msb - f->lsb - hi_bit;
  65. value <<= low_bit;
  66. low_bit -= 32;
  67. value |= (u64)data[hi_pos - 1] << low_bit;
  68. mask = GENMASK(31, 32 - low_bit);
  69. value |= (data[hi_pos - 2] & mask) >> low_bit;
  70. }
  71. return value;
  72. }
  73. static void qdma_set_field(const struct qdma_device *qdev, u32 *data,
  74. enum qdma_reg_fields field, u64 value)
  75. {
  76. const struct qdma_reg_field *f = &qdev->rfields[field];
  77. u16 low_pos, hi_pos, low_bit;
  78. low_pos = f->lsb / BITS_PER_TYPE(*data);
  79. hi_pos = f->msb / BITS_PER_TYPE(*data);
  80. low_bit = f->lsb % BITS_PER_TYPE(*data);
  81. data[low_pos++] |= value << low_bit;
  82. if (low_pos <= hi_pos)
  83. data[low_pos++] |= (u32)(value >> (32 - low_bit));
  84. if (low_pos <= hi_pos)
  85. data[low_pos] |= (u32)(value >> (64 - low_bit));
  86. }
  87. static inline int qdma_reg_write(const struct qdma_device *qdev,
  88. const u32 *data, enum qdma_regs reg)
  89. {
  90. const struct qdma_reg *r = &qdev->roffs[reg];
  91. int ret;
  92. if (r->count > 1)
  93. ret = regmap_bulk_write(qdev->regmap, r->off, data, r->count);
  94. else
  95. ret = regmap_write(qdev->regmap, r->off, *data);
  96. return ret;
  97. }
  98. static inline int qdma_reg_read(const struct qdma_device *qdev, u32 *data,
  99. enum qdma_regs reg)
  100. {
  101. const struct qdma_reg *r = &qdev->roffs[reg];
  102. int ret;
  103. if (r->count > 1)
  104. ret = regmap_bulk_read(qdev->regmap, r->off, data, r->count);
  105. else
  106. ret = regmap_read(qdev->regmap, r->off, data);
  107. return ret;
  108. }
  109. static int qdma_context_cmd_execute(const struct qdma_device *qdev,
  110. enum qdma_ctxt_type type,
  111. enum qdma_ctxt_cmd cmd, u16 index)
  112. {
  113. u32 value = 0;
  114. int ret;
  115. qdma_set_field(qdev, &value, QDMA_REGF_CMD_INDX, index);
  116. qdma_set_field(qdev, &value, QDMA_REGF_CMD_CMD, cmd);
  117. qdma_set_field(qdev, &value, QDMA_REGF_CMD_TYPE, type);
  118. ret = qdma_reg_write(qdev, &value, QDMA_REGO_CTXT_CMD);
  119. if (ret)
  120. return ret;
  121. ret = regmap_read_poll_timeout(qdev->regmap,
  122. QDMA_REG_OFF(qdev, QDMA_REGO_CTXT_CMD),
  123. value,
  124. !qdma_get_field(qdev, &value,
  125. QDMA_REGF_CMD_BUSY),
  126. QDMA_POLL_INTRVL_US,
  127. QDMA_POLL_TIMEOUT_US);
  128. if (ret) {
  129. qdma_err(qdev, "Context command execution timed out");
  130. return ret;
  131. }
  132. return 0;
  133. }
  134. static int qdma_context_write_data(const struct qdma_device *qdev,
  135. const u32 *data)
  136. {
  137. u32 mask[QDMA_CTXT_REGMAP_LEN];
  138. int ret;
  139. memset(mask, ~0, sizeof(mask));
  140. ret = qdma_reg_write(qdev, mask, QDMA_REGO_CTXT_MASK);
  141. if (ret)
  142. return ret;
  143. ret = qdma_reg_write(qdev, data, QDMA_REGO_CTXT_DATA);
  144. if (ret)
  145. return ret;
  146. return 0;
  147. }
  148. static void qdma_prep_sw_desc_context(const struct qdma_device *qdev,
  149. const struct qdma_ctxt_sw_desc *ctxt,
  150. u32 *data)
  151. {
  152. memset(data, 0, QDMA_CTXT_REGMAP_LEN * sizeof(*data));
  153. qdma_set_field(qdev, data, QDMA_REGF_DESC_BASE, ctxt->desc_base);
  154. qdma_set_field(qdev, data, QDMA_REGF_IRQ_VEC, ctxt->vec);
  155. qdma_set_field(qdev, data, QDMA_REGF_FUNCTION_ID, qdev->fid);
  156. qdma_set_field(qdev, data, QDMA_REGF_DESC_SIZE, QDMA_DESC_SIZE_32B);
  157. qdma_set_field(qdev, data, QDMA_REGF_RING_ID, QDMA_DEFAULT_RING_ID);
  158. qdma_set_field(qdev, data, QDMA_REGF_QUEUE_MODE, QDMA_QUEUE_OP_MM);
  159. qdma_set_field(qdev, data, QDMA_REGF_IRQ_ENABLE, 1);
  160. qdma_set_field(qdev, data, QDMA_REGF_WBK_ENABLE, 1);
  161. qdma_set_field(qdev, data, QDMA_REGF_WBI_CHECK, 1);
  162. qdma_set_field(qdev, data, QDMA_REGF_IRQ_ARM, 1);
  163. qdma_set_field(qdev, data, QDMA_REGF_IRQ_AGG, 1);
  164. qdma_set_field(qdev, data, QDMA_REGF_WBI_INTVL_ENABLE, 1);
  165. qdma_set_field(qdev, data, QDMA_REGF_QUEUE_ENABLE, 1);
  166. qdma_set_field(qdev, data, QDMA_REGF_MRKR_DISABLE, 1);
  167. }
  168. static void qdma_prep_intr_context(const struct qdma_device *qdev,
  169. const struct qdma_ctxt_intr *ctxt,
  170. u32 *data)
  171. {
  172. memset(data, 0, QDMA_CTXT_REGMAP_LEN * sizeof(*data));
  173. qdma_set_field(qdev, data, QDMA_REGF_INTR_AGG_BASE, ctxt->agg_base);
  174. qdma_set_field(qdev, data, QDMA_REGF_INTR_VECTOR, ctxt->vec);
  175. qdma_set_field(qdev, data, QDMA_REGF_INTR_SIZE, ctxt->size);
  176. qdma_set_field(qdev, data, QDMA_REGF_INTR_VALID, ctxt->valid);
  177. qdma_set_field(qdev, data, QDMA_REGF_INTR_COLOR, ctxt->color);
  178. qdma_set_field(qdev, data, QDMA_REGF_INTR_FUNCTION_ID, qdev->fid);
  179. }
  180. static void qdma_prep_fmap_context(const struct qdma_device *qdev,
  181. const struct qdma_ctxt_fmap *ctxt,
  182. u32 *data)
  183. {
  184. memset(data, 0, QDMA_CTXT_REGMAP_LEN * sizeof(*data));
  185. qdma_set_field(qdev, data, QDMA_REGF_QUEUE_BASE, ctxt->qbase);
  186. qdma_set_field(qdev, data, QDMA_REGF_QUEUE_MAX, ctxt->qmax);
  187. }
  188. /*
  189. * Program the indirect context register space
  190. *
  191. * Once the queue is enabled, context is dynamically updated by hardware. Any
  192. * modification of the context through this API when the queue is enabled can
  193. * result in unexpected behavior. Reading the context when the queue is enabled
  194. * is not recommended as it can result in reduced performance.
  195. */
  196. static int qdma_prog_context(struct qdma_device *qdev, enum qdma_ctxt_type type,
  197. enum qdma_ctxt_cmd cmd, u16 index, u32 *ctxt)
  198. {
  199. int ret;
  200. mutex_lock(&qdev->ctxt_lock);
  201. if (cmd == QDMA_CTXT_WRITE) {
  202. ret = qdma_context_write_data(qdev, ctxt);
  203. if (ret)
  204. goto failed;
  205. }
  206. ret = qdma_context_cmd_execute(qdev, type, cmd, index);
  207. if (ret)
  208. goto failed;
  209. if (cmd == QDMA_CTXT_READ) {
  210. ret = qdma_reg_read(qdev, ctxt, QDMA_REGO_CTXT_DATA);
  211. if (ret)
  212. goto failed;
  213. }
  214. failed:
  215. mutex_unlock(&qdev->ctxt_lock);
  216. return ret;
  217. }
  218. static int qdma_check_queue_status(struct qdma_device *qdev,
  219. enum dma_transfer_direction dir, u16 qid)
  220. {
  221. u32 status, data[QDMA_CTXT_REGMAP_LEN] = {0};
  222. enum qdma_ctxt_type type;
  223. int ret;
  224. if (dir == DMA_MEM_TO_DEV)
  225. type = QDMA_CTXT_DESC_SW_H2C;
  226. else
  227. type = QDMA_CTXT_DESC_SW_C2H;
  228. ret = qdma_prog_context(qdev, type, QDMA_CTXT_READ, qid, data);
  229. if (ret)
  230. return ret;
  231. status = qdma_get_field(qdev, data, QDMA_REGF_QUEUE_ENABLE);
  232. if (status) {
  233. qdma_err(qdev, "queue %d already in use", qid);
  234. return -EBUSY;
  235. }
  236. return 0;
  237. }
  238. static int qdma_clear_queue_context(const struct qdma_queue *queue)
  239. {
  240. enum qdma_ctxt_type h2c_types[] = { QDMA_CTXT_DESC_SW_H2C,
  241. QDMA_CTXT_DESC_HW_H2C,
  242. QDMA_CTXT_DESC_CR_H2C,
  243. QDMA_CTXT_PFTCH, };
  244. enum qdma_ctxt_type c2h_types[] = { QDMA_CTXT_DESC_SW_C2H,
  245. QDMA_CTXT_DESC_HW_C2H,
  246. QDMA_CTXT_DESC_CR_C2H,
  247. QDMA_CTXT_PFTCH, };
  248. struct qdma_device *qdev = queue->qdev;
  249. enum qdma_ctxt_type *type;
  250. int ret, num, i;
  251. if (queue->dir == DMA_MEM_TO_DEV) {
  252. type = h2c_types;
  253. num = ARRAY_SIZE(h2c_types);
  254. } else {
  255. type = c2h_types;
  256. num = ARRAY_SIZE(c2h_types);
  257. }
  258. for (i = 0; i < num; i++) {
  259. ret = qdma_prog_context(qdev, type[i], QDMA_CTXT_CLEAR,
  260. queue->qid, NULL);
  261. if (ret) {
  262. qdma_err(qdev, "Failed to clear ctxt %d", type[i]);
  263. return ret;
  264. }
  265. }
  266. return 0;
  267. }
  268. static int qdma_setup_fmap_context(struct qdma_device *qdev)
  269. {
  270. u32 ctxt[QDMA_CTXT_REGMAP_LEN];
  271. struct qdma_ctxt_fmap fmap;
  272. int ret;
  273. ret = qdma_prog_context(qdev, QDMA_CTXT_FMAP, QDMA_CTXT_CLEAR,
  274. qdev->fid, NULL);
  275. if (ret) {
  276. qdma_err(qdev, "Failed clearing context");
  277. return ret;
  278. }
  279. fmap.qbase = 0;
  280. fmap.qmax = qdev->chan_num * 2;
  281. qdma_prep_fmap_context(qdev, &fmap, ctxt);
  282. ret = qdma_prog_context(qdev, QDMA_CTXT_FMAP, QDMA_CTXT_WRITE,
  283. qdev->fid, ctxt);
  284. if (ret)
  285. qdma_err(qdev, "Failed setup fmap, ret %d", ret);
  286. return ret;
  287. }
  288. static int qdma_setup_queue_context(struct qdma_device *qdev,
  289. const struct qdma_ctxt_sw_desc *sw_desc,
  290. enum dma_transfer_direction dir, u16 qid)
  291. {
  292. u32 ctxt[QDMA_CTXT_REGMAP_LEN];
  293. enum qdma_ctxt_type type;
  294. int ret;
  295. if (dir == DMA_MEM_TO_DEV)
  296. type = QDMA_CTXT_DESC_SW_H2C;
  297. else
  298. type = QDMA_CTXT_DESC_SW_C2H;
  299. qdma_prep_sw_desc_context(qdev, sw_desc, ctxt);
  300. /* Setup SW descriptor context */
  301. ret = qdma_prog_context(qdev, type, QDMA_CTXT_WRITE, qid, ctxt);
  302. if (ret)
  303. qdma_err(qdev, "Failed setup SW desc ctxt for queue: %d", qid);
  304. return ret;
  305. }
  306. /*
  307. * Enable or disable memory-mapped DMA engines
  308. * 1: enable, 0: disable
  309. */
  310. static int qdma_sgdma_control(struct qdma_device *qdev, u32 ctrl)
  311. {
  312. int ret;
  313. ret = qdma_reg_write(qdev, &ctrl, QDMA_REGO_MM_H2C_CTRL);
  314. ret |= qdma_reg_write(qdev, &ctrl, QDMA_REGO_MM_C2H_CTRL);
  315. return ret;
  316. }
  317. static int qdma_get_hw_info(struct qdma_device *qdev)
  318. {
  319. struct qdma_platdata *pdata = dev_get_platdata(&qdev->pdev->dev);
  320. u32 value = 0;
  321. int ret;
  322. ret = qdma_reg_read(qdev, &value, QDMA_REGO_QUEUE_COUNT);
  323. if (ret)
  324. return ret;
  325. value = qdma_get_field(qdev, &value, QDMA_REGF_QUEUE_COUNT) + 1;
  326. if (pdata->max_mm_channels * 2 > value) {
  327. qdma_err(qdev, "not enough hw queues %d", value);
  328. return -EINVAL;
  329. }
  330. qdev->chan_num = pdata->max_mm_channels;
  331. ret = qdma_reg_read(qdev, &qdev->fid, QDMA_REGO_FUNC_ID);
  332. if (ret)
  333. return ret;
  334. qdma_info(qdev, "max channel %d, function id %d",
  335. qdev->chan_num, qdev->fid);
  336. return 0;
  337. }
  338. static inline int qdma_update_pidx(const struct qdma_queue *queue, u16 pidx)
  339. {
  340. struct qdma_device *qdev = queue->qdev;
  341. return regmap_write(qdev->regmap, queue->pidx_reg,
  342. pidx | QDMA_QUEUE_ARM_BIT);
  343. }
  344. static inline int qdma_update_cidx(const struct qdma_queue *queue,
  345. u16 ridx, u16 cidx)
  346. {
  347. struct qdma_device *qdev = queue->qdev;
  348. return regmap_write(qdev->regmap, queue->cidx_reg,
  349. ((u32)ridx << 16) | cidx);
  350. }
  351. /**
  352. * qdma_free_vdesc - Free descriptor
  353. * @vdesc: Virtual DMA descriptor
  354. */
  355. static void qdma_free_vdesc(struct virt_dma_desc *vdesc)
  356. {
  357. struct qdma_mm_vdesc *vd = to_qdma_vdesc(vdesc);
  358. kfree(vd);
  359. }
  360. static int qdma_alloc_queues(struct qdma_device *qdev,
  361. enum dma_transfer_direction dir)
  362. {
  363. struct qdma_queue *q, **queues;
  364. u32 i, pidx_base;
  365. int ret;
  366. if (dir == DMA_MEM_TO_DEV) {
  367. queues = &qdev->h2c_queues;
  368. pidx_base = QDMA_REG_OFF(qdev, QDMA_REGO_H2C_PIDX);
  369. } else {
  370. queues = &qdev->c2h_queues;
  371. pidx_base = QDMA_REG_OFF(qdev, QDMA_REGO_C2H_PIDX);
  372. }
  373. *queues = devm_kcalloc(&qdev->pdev->dev, qdev->chan_num, sizeof(*q),
  374. GFP_KERNEL);
  375. if (!*queues)
  376. return -ENOMEM;
  377. for (i = 0; i < qdev->chan_num; i++) {
  378. ret = qdma_check_queue_status(qdev, dir, i);
  379. if (ret)
  380. return ret;
  381. q = &(*queues)[i];
  382. q->ring_size = QDMA_DEFAULT_RING_SIZE;
  383. q->idx_mask = q->ring_size - 2;
  384. q->qdev = qdev;
  385. q->dir = dir;
  386. q->qid = i;
  387. q->pidx_reg = pidx_base + i * QDMA_DMAP_REG_STRIDE;
  388. q->cidx_reg = QDMA_REG_OFF(qdev, QDMA_REGO_INTR_CIDX) +
  389. i * QDMA_DMAP_REG_STRIDE;
  390. q->vchan.desc_free = qdma_free_vdesc;
  391. vchan_init(&q->vchan, &qdev->dma_dev);
  392. }
  393. return 0;
  394. }
  395. static int qdma_device_verify(struct qdma_device *qdev)
  396. {
  397. u32 value;
  398. int ret;
  399. ret = regmap_read(qdev->regmap, QDMA_IDENTIFIER_REGOFF, &value);
  400. if (ret)
  401. return ret;
  402. value = FIELD_GET(QDMA_IDENTIFIER_MASK, value);
  403. if (value != QDMA_IDENTIFIER) {
  404. qdma_err(qdev, "Invalid identifier");
  405. return -ENODEV;
  406. }
  407. qdev->rfields = qdma_regfs_default;
  408. qdev->roffs = qdma_regos_default;
  409. return 0;
  410. }
  411. static int qdma_device_setup(struct qdma_device *qdev)
  412. {
  413. u32 ring_sz = QDMA_DEFAULT_RING_SIZE;
  414. int ret = 0;
  415. ret = qdma_setup_fmap_context(qdev);
  416. if (ret) {
  417. qdma_err(qdev, "Failed setup fmap context");
  418. return ret;
  419. }
  420. /* Setup global ring buffer size at QDMA_DEFAULT_RING_ID index */
  421. ret = qdma_reg_write(qdev, &ring_sz, QDMA_REGO_RING_SIZE);
  422. if (ret) {
  423. qdma_err(qdev, "Failed to setup ring %d of size %ld",
  424. QDMA_DEFAULT_RING_ID, QDMA_DEFAULT_RING_SIZE);
  425. return ret;
  426. }
  427. /* Enable memory-mapped DMA engine in both directions */
  428. ret = qdma_sgdma_control(qdev, 1);
  429. if (ret) {
  430. qdma_err(qdev, "Failed to SGDMA with error %d", ret);
  431. return ret;
  432. }
  433. ret = qdma_alloc_queues(qdev, DMA_MEM_TO_DEV);
  434. if (ret) {
  435. qdma_err(qdev, "Failed to alloc H2C queues, ret %d", ret);
  436. return ret;
  437. }
  438. ret = qdma_alloc_queues(qdev, DMA_DEV_TO_MEM);
  439. if (ret) {
  440. qdma_err(qdev, "Failed to alloc C2H queues, ret %d", ret);
  441. return ret;
  442. }
  443. return 0;
  444. }
  445. /**
  446. * qdma_free_queue_resources() - Free queue resources
  447. * @chan: DMA channel
  448. */
  449. static void qdma_free_queue_resources(struct dma_chan *chan)
  450. {
  451. struct qdma_queue *queue = to_qdma_queue(chan);
  452. struct qdma_device *qdev = queue->qdev;
  453. struct qdma_platdata *pdata;
  454. qdma_clear_queue_context(queue);
  455. vchan_free_chan_resources(&queue->vchan);
  456. pdata = dev_get_platdata(&qdev->pdev->dev);
  457. dma_free_coherent(pdata->dma_dev, queue->ring_size * QDMA_MM_DESC_SIZE,
  458. queue->desc_base, queue->dma_desc_base);
  459. }
  460. /**
  461. * qdma_alloc_queue_resources() - Allocate queue resources
  462. * @chan: DMA channel
  463. */
  464. static int qdma_alloc_queue_resources(struct dma_chan *chan)
  465. {
  466. struct qdma_queue *queue = to_qdma_queue(chan);
  467. struct qdma_device *qdev = queue->qdev;
  468. struct qdma_ctxt_sw_desc desc;
  469. struct qdma_platdata *pdata;
  470. size_t size;
  471. int ret;
  472. ret = qdma_clear_queue_context(queue);
  473. if (ret)
  474. return ret;
  475. pdata = dev_get_platdata(&qdev->pdev->dev);
  476. size = queue->ring_size * QDMA_MM_DESC_SIZE;
  477. queue->desc_base = dma_alloc_coherent(pdata->dma_dev, size,
  478. &queue->dma_desc_base,
  479. GFP_KERNEL);
  480. if (!queue->desc_base) {
  481. qdma_err(qdev, "Failed to allocate descriptor ring");
  482. return -ENOMEM;
  483. }
  484. /* Setup SW descriptor queue context for DMA memory map */
  485. desc.vec = qdma_get_intr_ring_idx(qdev);
  486. desc.desc_base = queue->dma_desc_base;
  487. ret = qdma_setup_queue_context(qdev, &desc, queue->dir, queue->qid);
  488. if (ret) {
  489. qdma_err(qdev, "Failed to setup SW desc ctxt for %s",
  490. chan->name);
  491. dma_free_coherent(pdata->dma_dev, size, queue->desc_base,
  492. queue->dma_desc_base);
  493. return ret;
  494. }
  495. queue->pidx = 0;
  496. queue->cidx = 0;
  497. return 0;
  498. }
  499. static bool qdma_filter_fn(struct dma_chan *chan, void *param)
  500. {
  501. struct qdma_queue *queue = to_qdma_queue(chan);
  502. struct qdma_queue_info *info = param;
  503. return info->dir == queue->dir;
  504. }
  505. static int qdma_xfer_start(struct qdma_queue *queue)
  506. {
  507. struct qdma_device *qdev = queue->qdev;
  508. int ret;
  509. if (!vchan_next_desc(&queue->vchan))
  510. return 0;
  511. qdma_dbg(qdev, "Tnx kickoff with P: %d for %s%d",
  512. queue->issued_vdesc->pidx, CHAN_STR(queue), queue->qid);
  513. ret = qdma_update_pidx(queue, queue->issued_vdesc->pidx);
  514. if (ret) {
  515. qdma_err(qdev, "Failed to update PIDX to %d for %s queue: %d",
  516. queue->pidx, CHAN_STR(queue), queue->qid);
  517. }
  518. return ret;
  519. }
  520. static void qdma_issue_pending(struct dma_chan *chan)
  521. {
  522. struct qdma_queue *queue = to_qdma_queue(chan);
  523. unsigned long flags;
  524. spin_lock_irqsave(&queue->vchan.lock, flags);
  525. if (vchan_issue_pending(&queue->vchan)) {
  526. if (queue->submitted_vdesc) {
  527. queue->issued_vdesc = queue->submitted_vdesc;
  528. queue->submitted_vdesc = NULL;
  529. }
  530. qdma_xfer_start(queue);
  531. }
  532. spin_unlock_irqrestore(&queue->vchan.lock, flags);
  533. }
  534. static struct qdma_mm_desc *qdma_get_desc(struct qdma_queue *q)
  535. {
  536. struct qdma_mm_desc *desc;
  537. if (((q->pidx + 1) & q->idx_mask) == q->cidx)
  538. return NULL;
  539. desc = q->desc_base + q->pidx;
  540. q->pidx = (q->pidx + 1) & q->idx_mask;
  541. return desc;
  542. }
  543. static int qdma_hw_enqueue(struct qdma_queue *q, struct qdma_mm_vdesc *vdesc)
  544. {
  545. struct qdma_mm_desc *desc;
  546. struct scatterlist *sg;
  547. u64 addr, *src, *dst;
  548. u32 rest, len;
  549. int ret = 0;
  550. u32 i;
  551. if (!vdesc->sg_len)
  552. return 0;
  553. if (q->dir == DMA_MEM_TO_DEV) {
  554. dst = &vdesc->dev_addr;
  555. src = &addr;
  556. } else {
  557. dst = &addr;
  558. src = &vdesc->dev_addr;
  559. }
  560. for_each_sg(vdesc->sgl, sg, vdesc->sg_len, i) {
  561. addr = sg_dma_address(sg) + vdesc->sg_off;
  562. rest = sg_dma_len(sg) - vdesc->sg_off;
  563. while (rest) {
  564. len = min_t(u32, rest, QDMA_MM_DESC_MAX_LEN);
  565. desc = qdma_get_desc(q);
  566. if (!desc) {
  567. ret = -EBUSY;
  568. goto out;
  569. }
  570. desc->src_addr = cpu_to_le64(*src);
  571. desc->dst_addr = cpu_to_le64(*dst);
  572. desc->len = cpu_to_le32(len);
  573. vdesc->dev_addr += len;
  574. vdesc->sg_off += len;
  575. vdesc->pending_descs++;
  576. addr += len;
  577. rest -= len;
  578. }
  579. vdesc->sg_off = 0;
  580. }
  581. out:
  582. vdesc->sg_len -= i;
  583. vdesc->pidx = q->pidx;
  584. return ret;
  585. }
  586. static void qdma_fill_pending_vdesc(struct qdma_queue *q)
  587. {
  588. struct virt_dma_chan *vc = &q->vchan;
  589. struct qdma_mm_vdesc *vdesc = NULL;
  590. struct virt_dma_desc *vd;
  591. int ret;
  592. if (!list_empty(&vc->desc_issued)) {
  593. vd = &q->issued_vdesc->vdesc;
  594. list_for_each_entry_from(vd, &vc->desc_issued, node) {
  595. vdesc = to_qdma_vdesc(vd);
  596. ret = qdma_hw_enqueue(q, vdesc);
  597. if (ret) {
  598. q->issued_vdesc = vdesc;
  599. return;
  600. }
  601. }
  602. q->issued_vdesc = vdesc;
  603. }
  604. if (list_empty(&vc->desc_submitted))
  605. return;
  606. if (q->submitted_vdesc)
  607. vd = &q->submitted_vdesc->vdesc;
  608. else
  609. vd = list_first_entry(&vc->desc_submitted, typeof(*vd), node);
  610. list_for_each_entry_from(vd, &vc->desc_submitted, node) {
  611. vdesc = to_qdma_vdesc(vd);
  612. ret = qdma_hw_enqueue(q, vdesc);
  613. if (ret)
  614. break;
  615. }
  616. q->submitted_vdesc = vdesc;
  617. }
  618. static dma_cookie_t qdma_tx_submit(struct dma_async_tx_descriptor *tx)
  619. {
  620. struct virt_dma_chan *vc = to_virt_chan(tx->chan);
  621. struct qdma_queue *q = to_qdma_queue(&vc->chan);
  622. struct virt_dma_desc *vd;
  623. unsigned long flags;
  624. dma_cookie_t cookie;
  625. vd = container_of(tx, struct virt_dma_desc, tx);
  626. spin_lock_irqsave(&vc->lock, flags);
  627. cookie = dma_cookie_assign(tx);
  628. list_move_tail(&vd->node, &vc->desc_submitted);
  629. qdma_fill_pending_vdesc(q);
  630. spin_unlock_irqrestore(&vc->lock, flags);
  631. return cookie;
  632. }
  633. static struct dma_async_tx_descriptor *
  634. qdma_prep_device_sg(struct dma_chan *chan, struct scatterlist *sgl,
  635. unsigned int sg_len, enum dma_transfer_direction dir,
  636. unsigned long flags, void *context)
  637. {
  638. struct qdma_queue *q = to_qdma_queue(chan);
  639. struct dma_async_tx_descriptor *tx;
  640. struct qdma_mm_vdesc *vdesc;
  641. vdesc = kzalloc(sizeof(*vdesc), GFP_NOWAIT);
  642. if (!vdesc)
  643. return NULL;
  644. vdesc->sgl = sgl;
  645. vdesc->sg_len = sg_len;
  646. if (dir == DMA_MEM_TO_DEV)
  647. vdesc->dev_addr = q->cfg.dst_addr;
  648. else
  649. vdesc->dev_addr = q->cfg.src_addr;
  650. tx = vchan_tx_prep(&q->vchan, &vdesc->vdesc, flags);
  651. tx->tx_submit = qdma_tx_submit;
  652. return tx;
  653. }
  654. static int qdma_device_config(struct dma_chan *chan,
  655. struct dma_slave_config *cfg)
  656. {
  657. struct qdma_queue *q = to_qdma_queue(chan);
  658. memcpy(&q->cfg, cfg, sizeof(*cfg));
  659. return 0;
  660. }
  661. static int qdma_arm_err_intr(const struct qdma_device *qdev)
  662. {
  663. u32 value = 0;
  664. qdma_set_field(qdev, &value, QDMA_REGF_ERR_INT_FUNC, qdev->fid);
  665. qdma_set_field(qdev, &value, QDMA_REGF_ERR_INT_VEC, qdev->err_irq_idx);
  666. qdma_set_field(qdev, &value, QDMA_REGF_ERR_INT_ARM, 1);
  667. return qdma_reg_write(qdev, &value, QDMA_REGO_ERR_INT);
  668. }
  669. static irqreturn_t qdma_error_isr(int irq, void *data)
  670. {
  671. struct qdma_device *qdev = data;
  672. u32 err_stat = 0;
  673. int ret;
  674. ret = qdma_reg_read(qdev, &err_stat, QDMA_REGO_ERR_STAT);
  675. if (ret) {
  676. qdma_err(qdev, "read error state failed, ret %d", ret);
  677. goto out;
  678. }
  679. qdma_err(qdev, "global error %d", err_stat);
  680. ret = qdma_reg_write(qdev, &err_stat, QDMA_REGO_ERR_STAT);
  681. if (ret)
  682. qdma_err(qdev, "clear error state failed, ret %d", ret);
  683. out:
  684. qdma_arm_err_intr(qdev);
  685. return IRQ_HANDLED;
  686. }
  687. static irqreturn_t qdma_queue_isr(int irq, void *data)
  688. {
  689. struct qdma_intr_ring *intr = data;
  690. struct qdma_queue *q = NULL;
  691. struct qdma_device *qdev;
  692. u32 index, comp_desc;
  693. u64 intr_ent;
  694. u8 color;
  695. int ret;
  696. u16 qid;
  697. qdev = intr->qdev;
  698. index = intr->cidx;
  699. while (1) {
  700. struct virt_dma_desc *vd;
  701. struct qdma_mm_vdesc *vdesc;
  702. unsigned long flags;
  703. u32 cidx;
  704. intr_ent = le64_to_cpu(intr->base[index]);
  705. color = FIELD_GET(QDMA_INTR_MASK_COLOR, intr_ent);
  706. if (color != intr->color)
  707. break;
  708. qid = FIELD_GET(QDMA_INTR_MASK_QID, intr_ent);
  709. if (FIELD_GET(QDMA_INTR_MASK_TYPE, intr_ent))
  710. q = qdev->c2h_queues;
  711. else
  712. q = qdev->h2c_queues;
  713. q += qid;
  714. cidx = FIELD_GET(QDMA_INTR_MASK_CIDX, intr_ent);
  715. spin_lock_irqsave(&q->vchan.lock, flags);
  716. comp_desc = (cidx - q->cidx) & q->idx_mask;
  717. vd = vchan_next_desc(&q->vchan);
  718. if (!vd)
  719. goto skip;
  720. vdesc = to_qdma_vdesc(vd);
  721. while (comp_desc > vdesc->pending_descs) {
  722. list_del(&vd->node);
  723. vchan_cookie_complete(vd);
  724. comp_desc -= vdesc->pending_descs;
  725. vd = vchan_next_desc(&q->vchan);
  726. vdesc = to_qdma_vdesc(vd);
  727. }
  728. vdesc->pending_descs -= comp_desc;
  729. if (!vdesc->pending_descs && QDMA_VDESC_QUEUED(vdesc)) {
  730. list_del(&vd->node);
  731. vchan_cookie_complete(vd);
  732. }
  733. q->cidx = cidx;
  734. qdma_fill_pending_vdesc(q);
  735. qdma_xfer_start(q);
  736. skip:
  737. spin_unlock_irqrestore(&q->vchan.lock, flags);
  738. /*
  739. * Wrap the index value and flip the expected color value if
  740. * interrupt aggregation PIDX has wrapped around.
  741. */
  742. index++;
  743. index &= QDMA_INTR_RING_IDX_MASK;
  744. if (!index)
  745. intr->color = !intr->color;
  746. }
  747. /*
  748. * Update the software interrupt aggregation ring CIDX if a valid entry
  749. * was found.
  750. */
  751. if (q) {
  752. qdma_dbg(qdev, "update intr ring%d %d", intr->ridx, index);
  753. /*
  754. * Record the last read index of status descriptor from the
  755. * interrupt aggregation ring.
  756. */
  757. intr->cidx = index;
  758. ret = qdma_update_cidx(q, intr->ridx, index);
  759. if (ret) {
  760. qdma_err(qdev, "Failed to update IRQ CIDX");
  761. return IRQ_NONE;
  762. }
  763. }
  764. return IRQ_HANDLED;
  765. }
  766. static int qdma_init_error_irq(struct qdma_device *qdev)
  767. {
  768. struct device *dev = &qdev->pdev->dev;
  769. int ret;
  770. u32 vec;
  771. vec = qdev->queue_irq_start - 1;
  772. ret = devm_request_threaded_irq(dev, vec, NULL, qdma_error_isr,
  773. IRQF_ONESHOT, "amd-qdma-error", qdev);
  774. if (ret) {
  775. qdma_err(qdev, "Failed to request error IRQ vector: %d", vec);
  776. return ret;
  777. }
  778. ret = qdma_arm_err_intr(qdev);
  779. if (ret)
  780. qdma_err(qdev, "Failed to arm err interrupt, ret %d", ret);
  781. return ret;
  782. }
  783. static int qdmam_alloc_qintr_rings(struct qdma_device *qdev)
  784. {
  785. struct qdma_platdata *pdata = dev_get_platdata(&qdev->pdev->dev);
  786. struct device *dev = &qdev->pdev->dev;
  787. u32 ctxt[QDMA_CTXT_REGMAP_LEN];
  788. struct qdma_intr_ring *ring;
  789. struct qdma_ctxt_intr intr_ctxt;
  790. u32 vector;
  791. int ret, i;
  792. qdev->qintr_ring_num = qdev->queue_irq_num;
  793. qdev->qintr_rings = devm_kcalloc(dev, qdev->qintr_ring_num,
  794. sizeof(*qdev->qintr_rings),
  795. GFP_KERNEL);
  796. if (!qdev->qintr_rings)
  797. return -ENOMEM;
  798. vector = qdev->queue_irq_start;
  799. for (i = 0; i < qdev->qintr_ring_num; i++, vector++) {
  800. ring = &qdev->qintr_rings[i];
  801. ring->qdev = qdev;
  802. ring->msix_id = qdev->err_irq_idx + i + 1;
  803. ring->ridx = i;
  804. ring->color = 1;
  805. ring->base = dmam_alloc_coherent(pdata->dma_dev,
  806. QDMA_INTR_RING_SIZE,
  807. &ring->dev_base, GFP_KERNEL);
  808. if (!ring->base) {
  809. qdma_err(qdev, "Failed to alloc intr ring %d", i);
  810. return -ENOMEM;
  811. }
  812. intr_ctxt.agg_base = QDMA_INTR_RING_BASE(ring->dev_base);
  813. intr_ctxt.size = (QDMA_INTR_RING_SIZE - 1) / 4096;
  814. intr_ctxt.vec = ring->msix_id;
  815. intr_ctxt.valid = true;
  816. intr_ctxt.color = true;
  817. ret = qdma_prog_context(qdev, QDMA_CTXT_INTR_COAL,
  818. QDMA_CTXT_CLEAR, ring->ridx, NULL);
  819. if (ret) {
  820. qdma_err(qdev, "Failed clear intr ctx, ret %d", ret);
  821. return ret;
  822. }
  823. qdma_prep_intr_context(qdev, &intr_ctxt, ctxt);
  824. ret = qdma_prog_context(qdev, QDMA_CTXT_INTR_COAL,
  825. QDMA_CTXT_WRITE, ring->ridx, ctxt);
  826. if (ret) {
  827. qdma_err(qdev, "Failed setup intr ctx, ret %d", ret);
  828. return ret;
  829. }
  830. ret = devm_request_threaded_irq(dev, vector, NULL,
  831. qdma_queue_isr, IRQF_ONESHOT,
  832. "amd-qdma-queue", ring);
  833. if (ret) {
  834. qdma_err(qdev, "Failed to request irq %d", vector);
  835. return ret;
  836. }
  837. }
  838. return 0;
  839. }
  840. static int qdma_intr_init(struct qdma_device *qdev)
  841. {
  842. int ret;
  843. ret = qdma_init_error_irq(qdev);
  844. if (ret) {
  845. qdma_err(qdev, "Failed to init error IRQs, ret %d", ret);
  846. return ret;
  847. }
  848. ret = qdmam_alloc_qintr_rings(qdev);
  849. if (ret) {
  850. qdma_err(qdev, "Failed to init queue IRQs, ret %d", ret);
  851. return ret;
  852. }
  853. return 0;
  854. }
  855. static void amd_qdma_remove(struct platform_device *pdev)
  856. {
  857. struct qdma_device *qdev = platform_get_drvdata(pdev);
  858. qdma_sgdma_control(qdev, 0);
  859. dma_async_device_unregister(&qdev->dma_dev);
  860. mutex_destroy(&qdev->ctxt_lock);
  861. }
  862. static int amd_qdma_probe(struct platform_device *pdev)
  863. {
  864. struct qdma_platdata *pdata = dev_get_platdata(&pdev->dev);
  865. struct qdma_device *qdev;
  866. struct resource *res;
  867. void __iomem *regs;
  868. int ret;
  869. qdev = devm_kzalloc(&pdev->dev, sizeof(*qdev), GFP_KERNEL);
  870. if (!qdev)
  871. return -ENOMEM;
  872. platform_set_drvdata(pdev, qdev);
  873. qdev->pdev = pdev;
  874. mutex_init(&qdev->ctxt_lock);
  875. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  876. if (!res) {
  877. qdma_err(qdev, "Failed to get IRQ resource");
  878. ret = -ENODEV;
  879. goto failed;
  880. }
  881. qdev->err_irq_idx = pdata->irq_index;
  882. qdev->queue_irq_start = res->start + 1;
  883. qdev->queue_irq_num = resource_size(res) - 1;
  884. regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  885. if (IS_ERR(regs)) {
  886. ret = PTR_ERR(regs);
  887. qdma_err(qdev, "Failed to map IO resource, err %d", ret);
  888. goto failed;
  889. }
  890. qdev->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
  891. &qdma_regmap_config);
  892. if (IS_ERR(qdev->regmap)) {
  893. ret = PTR_ERR(qdev->regmap);
  894. qdma_err(qdev, "Regmap init failed, err %d", ret);
  895. goto failed;
  896. }
  897. ret = qdma_device_verify(qdev);
  898. if (ret)
  899. goto failed;
  900. ret = qdma_get_hw_info(qdev);
  901. if (ret)
  902. goto failed;
  903. INIT_LIST_HEAD(&qdev->dma_dev.channels);
  904. ret = qdma_device_setup(qdev);
  905. if (ret)
  906. goto failed;
  907. ret = qdma_intr_init(qdev);
  908. if (ret) {
  909. qdma_err(qdev, "Failed to initialize IRQs %d", ret);
  910. goto failed_disable_engine;
  911. }
  912. dma_cap_set(DMA_SLAVE, qdev->dma_dev.cap_mask);
  913. dma_cap_set(DMA_PRIVATE, qdev->dma_dev.cap_mask);
  914. qdev->dma_dev.dev = &pdev->dev;
  915. qdev->dma_dev.filter.map = pdata->device_map;
  916. qdev->dma_dev.filter.mapcnt = qdev->chan_num * 2;
  917. qdev->dma_dev.filter.fn = qdma_filter_fn;
  918. qdev->dma_dev.device_alloc_chan_resources = qdma_alloc_queue_resources;
  919. qdev->dma_dev.device_free_chan_resources = qdma_free_queue_resources;
  920. qdev->dma_dev.device_prep_slave_sg = qdma_prep_device_sg;
  921. qdev->dma_dev.device_config = qdma_device_config;
  922. qdev->dma_dev.device_issue_pending = qdma_issue_pending;
  923. qdev->dma_dev.device_tx_status = dma_cookie_status;
  924. qdev->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  925. ret = dma_async_device_register(&qdev->dma_dev);
  926. if (ret) {
  927. qdma_err(qdev, "Failed to register AMD QDMA: %d", ret);
  928. goto failed_disable_engine;
  929. }
  930. return 0;
  931. failed_disable_engine:
  932. qdma_sgdma_control(qdev, 0);
  933. failed:
  934. mutex_destroy(&qdev->ctxt_lock);
  935. qdma_err(qdev, "Failed to probe AMD QDMA driver");
  936. return ret;
  937. }
  938. static struct platform_driver amd_qdma_driver = {
  939. .driver = {
  940. .name = "amd-qdma",
  941. },
  942. .probe = amd_qdma_probe,
  943. .remove_new = amd_qdma_remove,
  944. };
  945. module_platform_driver(amd_qdma_driver);
  946. MODULE_DESCRIPTION("AMD QDMA driver");
  947. MODULE_AUTHOR("XRT Team <runtimeca39d@amd.com>");
  948. MODULE_LICENSE("GPL");