mvneta_bm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Driver for Marvell NETA network controller Buffer Manager.
  3. *
  4. * Copyright (C) 2015 Marvell
  5. *
  6. * Marcin Wojtas <mw@semihalf.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/genalloc.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mbus.h>
  17. #include <linux/module.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/skbuff.h>
  23. #include <net/hwbm.h>
  24. #include "mvneta_bm.h"
  25. #define MVNETA_BM_DRIVER_NAME "mvneta_bm"
  26. #define MVNETA_BM_DRIVER_VERSION "1.0"
  27. static void mvneta_bm_write(struct mvneta_bm *priv, u32 offset, u32 data)
  28. {
  29. writel(data, priv->reg_base + offset);
  30. }
  31. static u32 mvneta_bm_read(struct mvneta_bm *priv, u32 offset)
  32. {
  33. return readl(priv->reg_base + offset);
  34. }
  35. static void mvneta_bm_pool_enable(struct mvneta_bm *priv, int pool_id)
  36. {
  37. u32 val;
  38. val = mvneta_bm_read(priv, MVNETA_BM_POOL_BASE_REG(pool_id));
  39. val |= MVNETA_BM_POOL_ENABLE_MASK;
  40. mvneta_bm_write(priv, MVNETA_BM_POOL_BASE_REG(pool_id), val);
  41. /* Clear BM cause register */
  42. mvneta_bm_write(priv, MVNETA_BM_INTR_CAUSE_REG, 0);
  43. }
  44. static void mvneta_bm_pool_disable(struct mvneta_bm *priv, int pool_id)
  45. {
  46. u32 val;
  47. val = mvneta_bm_read(priv, MVNETA_BM_POOL_BASE_REG(pool_id));
  48. val &= ~MVNETA_BM_POOL_ENABLE_MASK;
  49. mvneta_bm_write(priv, MVNETA_BM_POOL_BASE_REG(pool_id), val);
  50. }
  51. static inline void mvneta_bm_config_set(struct mvneta_bm *priv, u32 mask)
  52. {
  53. u32 val;
  54. val = mvneta_bm_read(priv, MVNETA_BM_CONFIG_REG);
  55. val |= mask;
  56. mvneta_bm_write(priv, MVNETA_BM_CONFIG_REG, val);
  57. }
  58. static inline void mvneta_bm_config_clear(struct mvneta_bm *priv, u32 mask)
  59. {
  60. u32 val;
  61. val = mvneta_bm_read(priv, MVNETA_BM_CONFIG_REG);
  62. val &= ~mask;
  63. mvneta_bm_write(priv, MVNETA_BM_CONFIG_REG, val);
  64. }
  65. static void mvneta_bm_pool_target_set(struct mvneta_bm *priv, int pool_id,
  66. u8 target_id, u8 attr)
  67. {
  68. u32 val;
  69. val = mvneta_bm_read(priv, MVNETA_BM_XBAR_POOL_REG(pool_id));
  70. val &= ~MVNETA_BM_TARGET_ID_MASK(pool_id);
  71. val &= ~MVNETA_BM_XBAR_ATTR_MASK(pool_id);
  72. val |= MVNETA_BM_TARGET_ID_VAL(pool_id, target_id);
  73. val |= MVNETA_BM_XBAR_ATTR_VAL(pool_id, attr);
  74. mvneta_bm_write(priv, MVNETA_BM_XBAR_POOL_REG(pool_id), val);
  75. }
  76. int mvneta_bm_construct(struct hwbm_pool *hwbm_pool, void *buf)
  77. {
  78. struct mvneta_bm_pool *bm_pool =
  79. (struct mvneta_bm_pool *)hwbm_pool->priv;
  80. struct mvneta_bm *priv = bm_pool->priv;
  81. dma_addr_t phys_addr;
  82. /* In order to update buf_cookie field of RX descriptor properly,
  83. * BM hardware expects buf virtual address to be placed in the
  84. * first four bytes of mapped buffer.
  85. */
  86. *(u32 *)buf = (u32)buf;
  87. phys_addr = dma_map_single(&priv->pdev->dev, buf, bm_pool->buf_size,
  88. DMA_FROM_DEVICE);
  89. if (unlikely(dma_mapping_error(&priv->pdev->dev, phys_addr)))
  90. return -ENOMEM;
  91. mvneta_bm_pool_put_bp(priv, bm_pool, phys_addr);
  92. return 0;
  93. }
  94. EXPORT_SYMBOL_GPL(mvneta_bm_construct);
  95. /* Create pool */
  96. static int mvneta_bm_pool_create(struct mvneta_bm *priv,
  97. struct mvneta_bm_pool *bm_pool)
  98. {
  99. struct platform_device *pdev = priv->pdev;
  100. u8 target_id, attr;
  101. int size_bytes, err;
  102. size_bytes = sizeof(u32) * bm_pool->hwbm_pool.size;
  103. bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, size_bytes,
  104. &bm_pool->phys_addr,
  105. GFP_KERNEL);
  106. if (!bm_pool->virt_addr)
  107. return -ENOMEM;
  108. if (!IS_ALIGNED((u32)bm_pool->virt_addr, MVNETA_BM_POOL_PTR_ALIGN)) {
  109. dma_free_coherent(&pdev->dev, size_bytes, bm_pool->virt_addr,
  110. bm_pool->phys_addr);
  111. dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
  112. bm_pool->id, MVNETA_BM_POOL_PTR_ALIGN);
  113. return -ENOMEM;
  114. }
  115. err = mvebu_mbus_get_dram_win_info(bm_pool->phys_addr, &target_id,
  116. &attr);
  117. if (err < 0) {
  118. dma_free_coherent(&pdev->dev, size_bytes, bm_pool->virt_addr,
  119. bm_pool->phys_addr);
  120. return err;
  121. }
  122. /* Set pool address */
  123. mvneta_bm_write(priv, MVNETA_BM_POOL_BASE_REG(bm_pool->id),
  124. bm_pool->phys_addr);
  125. mvneta_bm_pool_target_set(priv, bm_pool->id, target_id, attr);
  126. mvneta_bm_pool_enable(priv, bm_pool->id);
  127. return 0;
  128. }
  129. /* Notify the driver that BM pool is being used as specific type and return the
  130. * pool pointer on success
  131. */
  132. struct mvneta_bm_pool *mvneta_bm_pool_use(struct mvneta_bm *priv, u8 pool_id,
  133. enum mvneta_bm_type type, u8 port_id,
  134. int pkt_size)
  135. {
  136. struct mvneta_bm_pool *new_pool = &priv->bm_pools[pool_id];
  137. int num, err;
  138. if (new_pool->type == MVNETA_BM_LONG &&
  139. new_pool->port_map != 1 << port_id) {
  140. dev_err(&priv->pdev->dev,
  141. "long pool cannot be shared by the ports\n");
  142. return NULL;
  143. }
  144. if (new_pool->type == MVNETA_BM_SHORT && new_pool->type != type) {
  145. dev_err(&priv->pdev->dev,
  146. "mixing pools' types between the ports is forbidden\n");
  147. return NULL;
  148. }
  149. if (new_pool->pkt_size == 0 || type != MVNETA_BM_SHORT)
  150. new_pool->pkt_size = pkt_size;
  151. /* Allocate buffers in case BM pool hasn't been used yet */
  152. if (new_pool->type == MVNETA_BM_FREE) {
  153. struct hwbm_pool *hwbm_pool = &new_pool->hwbm_pool;
  154. new_pool->priv = priv;
  155. new_pool->type = type;
  156. new_pool->buf_size = MVNETA_RX_BUF_SIZE(new_pool->pkt_size);
  157. hwbm_pool->frag_size =
  158. SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(new_pool->pkt_size)) +
  159. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  160. hwbm_pool->construct = mvneta_bm_construct;
  161. hwbm_pool->priv = new_pool;
  162. spin_lock_init(&hwbm_pool->lock);
  163. /* Create new pool */
  164. err = mvneta_bm_pool_create(priv, new_pool);
  165. if (err) {
  166. dev_err(&priv->pdev->dev, "fail to create pool %d\n",
  167. new_pool->id);
  168. return NULL;
  169. }
  170. /* Allocate buffers for this pool */
  171. num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
  172. if (num != hwbm_pool->size) {
  173. WARN(1, "pool %d: %d of %d allocated\n",
  174. new_pool->id, num, hwbm_pool->size);
  175. return NULL;
  176. }
  177. }
  178. return new_pool;
  179. }
  180. EXPORT_SYMBOL_GPL(mvneta_bm_pool_use);
  181. /* Free all buffers from the pool */
  182. void mvneta_bm_bufs_free(struct mvneta_bm *priv, struct mvneta_bm_pool *bm_pool,
  183. u8 port_map)
  184. {
  185. int i;
  186. bm_pool->port_map &= ~port_map;
  187. if (bm_pool->port_map)
  188. return;
  189. mvneta_bm_config_set(priv, MVNETA_BM_EMPTY_LIMIT_MASK);
  190. for (i = 0; i < bm_pool->hwbm_pool.buf_num; i++) {
  191. dma_addr_t buf_phys_addr;
  192. u32 *vaddr;
  193. /* Get buffer physical address (indirect access) */
  194. buf_phys_addr = mvneta_bm_pool_get_bp(priv, bm_pool);
  195. /* Work-around to the problems when destroying the pool,
  196. * when it occurs that a read access to BPPI returns 0.
  197. */
  198. if (buf_phys_addr == 0)
  199. continue;
  200. vaddr = phys_to_virt(buf_phys_addr);
  201. if (!vaddr)
  202. break;
  203. dma_unmap_single(&priv->pdev->dev, buf_phys_addr,
  204. bm_pool->buf_size, DMA_FROM_DEVICE);
  205. hwbm_buf_free(&bm_pool->hwbm_pool, vaddr);
  206. }
  207. mvneta_bm_config_clear(priv, MVNETA_BM_EMPTY_LIMIT_MASK);
  208. /* Update BM driver with number of buffers removed from pool */
  209. bm_pool->hwbm_pool.buf_num -= i;
  210. }
  211. EXPORT_SYMBOL_GPL(mvneta_bm_bufs_free);
  212. /* Cleanup pool */
  213. void mvneta_bm_pool_destroy(struct mvneta_bm *priv,
  214. struct mvneta_bm_pool *bm_pool, u8 port_map)
  215. {
  216. struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
  217. bm_pool->port_map &= ~port_map;
  218. if (bm_pool->port_map)
  219. return;
  220. bm_pool->type = MVNETA_BM_FREE;
  221. mvneta_bm_bufs_free(priv, bm_pool, port_map);
  222. if (hwbm_pool->buf_num)
  223. WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
  224. if (bm_pool->virt_addr) {
  225. dma_free_coherent(&priv->pdev->dev,
  226. sizeof(u32) * hwbm_pool->size,
  227. bm_pool->virt_addr, bm_pool->phys_addr);
  228. bm_pool->virt_addr = NULL;
  229. }
  230. mvneta_bm_pool_disable(priv, bm_pool->id);
  231. }
  232. EXPORT_SYMBOL_GPL(mvneta_bm_pool_destroy);
  233. static void mvneta_bm_pools_init(struct mvneta_bm *priv)
  234. {
  235. struct device_node *dn = priv->pdev->dev.of_node;
  236. struct mvneta_bm_pool *bm_pool;
  237. char prop[15];
  238. u32 size;
  239. int i;
  240. /* Activate BM unit */
  241. mvneta_bm_write(priv, MVNETA_BM_COMMAND_REG, MVNETA_BM_START_MASK);
  242. /* Create all pools with maximum size */
  243. for (i = 0; i < MVNETA_BM_POOLS_NUM; i++) {
  244. bm_pool = &priv->bm_pools[i];
  245. bm_pool->id = i;
  246. bm_pool->type = MVNETA_BM_FREE;
  247. /* Reset read pointer */
  248. mvneta_bm_write(priv, MVNETA_BM_POOL_READ_PTR_REG(i), 0);
  249. /* Reset write pointer */
  250. mvneta_bm_write(priv, MVNETA_BM_POOL_WRITE_PTR_REG(i), 0);
  251. /* Configure pool size according to DT or use default value */
  252. sprintf(prop, "pool%d,capacity", i);
  253. if (of_property_read_u32(dn, prop, &size)) {
  254. size = MVNETA_BM_POOL_CAP_DEF;
  255. } else if (size > MVNETA_BM_POOL_CAP_MAX) {
  256. dev_warn(&priv->pdev->dev,
  257. "Illegal pool %d capacity %d, set to %d\n",
  258. i, size, MVNETA_BM_POOL_CAP_MAX);
  259. size = MVNETA_BM_POOL_CAP_MAX;
  260. } else if (size < MVNETA_BM_POOL_CAP_MIN) {
  261. dev_warn(&priv->pdev->dev,
  262. "Illegal pool %d capacity %d, set to %d\n",
  263. i, size, MVNETA_BM_POOL_CAP_MIN);
  264. size = MVNETA_BM_POOL_CAP_MIN;
  265. } else if (!IS_ALIGNED(size, MVNETA_BM_POOL_CAP_ALIGN)) {
  266. dev_warn(&priv->pdev->dev,
  267. "Illegal pool %d capacity %d, round to %d\n",
  268. i, size, ALIGN(size,
  269. MVNETA_BM_POOL_CAP_ALIGN));
  270. size = ALIGN(size, MVNETA_BM_POOL_CAP_ALIGN);
  271. }
  272. bm_pool->hwbm_pool.size = size;
  273. mvneta_bm_write(priv, MVNETA_BM_POOL_SIZE_REG(i),
  274. bm_pool->hwbm_pool.size);
  275. /* Obtain custom pkt_size from DT */
  276. sprintf(prop, "pool%d,pkt-size", i);
  277. if (of_property_read_u32(dn, prop, &bm_pool->pkt_size))
  278. bm_pool->pkt_size = 0;
  279. }
  280. }
  281. static void mvneta_bm_default_set(struct mvneta_bm *priv)
  282. {
  283. u32 val;
  284. /* Mask BM all interrupts */
  285. mvneta_bm_write(priv, MVNETA_BM_INTR_MASK_REG, 0);
  286. /* Clear BM cause register */
  287. mvneta_bm_write(priv, MVNETA_BM_INTR_CAUSE_REG, 0);
  288. /* Set BM configuration register */
  289. val = mvneta_bm_read(priv, MVNETA_BM_CONFIG_REG);
  290. /* Reduce MaxInBurstSize from 32 BPs to 16 BPs */
  291. val &= ~MVNETA_BM_MAX_IN_BURST_SIZE_MASK;
  292. val |= MVNETA_BM_MAX_IN_BURST_SIZE_16BP;
  293. mvneta_bm_write(priv, MVNETA_BM_CONFIG_REG, val);
  294. }
  295. static int mvneta_bm_init(struct mvneta_bm *priv)
  296. {
  297. mvneta_bm_default_set(priv);
  298. /* Allocate and initialize BM pools structures */
  299. priv->bm_pools = devm_kcalloc(&priv->pdev->dev, MVNETA_BM_POOLS_NUM,
  300. sizeof(struct mvneta_bm_pool),
  301. GFP_KERNEL);
  302. if (!priv->bm_pools)
  303. return -ENOMEM;
  304. mvneta_bm_pools_init(priv);
  305. return 0;
  306. }
  307. static int mvneta_bm_get_sram(struct device_node *dn,
  308. struct mvneta_bm *priv)
  309. {
  310. priv->bppi_pool = of_gen_pool_get(dn, "internal-mem", 0);
  311. if (!priv->bppi_pool)
  312. return -ENOMEM;
  313. priv->bppi_virt_addr = gen_pool_dma_alloc(priv->bppi_pool,
  314. MVNETA_BM_BPPI_SIZE,
  315. &priv->bppi_phys_addr);
  316. if (!priv->bppi_virt_addr)
  317. return -ENOMEM;
  318. return 0;
  319. }
  320. static void mvneta_bm_put_sram(struct mvneta_bm *priv)
  321. {
  322. gen_pool_free(priv->bppi_pool, priv->bppi_phys_addr,
  323. MVNETA_BM_BPPI_SIZE);
  324. }
  325. struct mvneta_bm *mvneta_bm_get(struct device_node *node)
  326. {
  327. struct platform_device *pdev = of_find_device_by_node(node);
  328. return pdev ? platform_get_drvdata(pdev) : NULL;
  329. }
  330. EXPORT_SYMBOL_GPL(mvneta_bm_get);
  331. void mvneta_bm_put(struct mvneta_bm *priv)
  332. {
  333. platform_device_put(priv->pdev);
  334. }
  335. EXPORT_SYMBOL_GPL(mvneta_bm_put);
  336. static int mvneta_bm_probe(struct platform_device *pdev)
  337. {
  338. struct device_node *dn = pdev->dev.of_node;
  339. struct mvneta_bm *priv;
  340. struct resource *res;
  341. int err;
  342. priv = devm_kzalloc(&pdev->dev, sizeof(struct mvneta_bm), GFP_KERNEL);
  343. if (!priv)
  344. return -ENOMEM;
  345. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  346. priv->reg_base = devm_ioremap_resource(&pdev->dev, res);
  347. if (IS_ERR(priv->reg_base))
  348. return PTR_ERR(priv->reg_base);
  349. priv->clk = devm_clk_get(&pdev->dev, NULL);
  350. if (IS_ERR(priv->clk))
  351. return PTR_ERR(priv->clk);
  352. err = clk_prepare_enable(priv->clk);
  353. if (err < 0)
  354. return err;
  355. err = mvneta_bm_get_sram(dn, priv);
  356. if (err < 0) {
  357. dev_err(&pdev->dev, "failed to allocate internal memory\n");
  358. goto err_clk;
  359. }
  360. priv->pdev = pdev;
  361. /* Initialize buffer manager internals */
  362. err = mvneta_bm_init(priv);
  363. if (err < 0) {
  364. dev_err(&pdev->dev, "failed to initialize controller\n");
  365. goto err_sram;
  366. }
  367. dn->data = priv;
  368. platform_set_drvdata(pdev, priv);
  369. dev_info(&pdev->dev, "Buffer Manager for network controller enabled\n");
  370. return 0;
  371. err_sram:
  372. mvneta_bm_put_sram(priv);
  373. err_clk:
  374. clk_disable_unprepare(priv->clk);
  375. return err;
  376. }
  377. static int mvneta_bm_remove(struct platform_device *pdev)
  378. {
  379. struct mvneta_bm *priv = platform_get_drvdata(pdev);
  380. u8 all_ports_map = 0xff;
  381. int i = 0;
  382. for (i = 0; i < MVNETA_BM_POOLS_NUM; i++) {
  383. struct mvneta_bm_pool *bm_pool = &priv->bm_pools[i];
  384. mvneta_bm_pool_destroy(priv, bm_pool, all_ports_map);
  385. }
  386. mvneta_bm_put_sram(priv);
  387. /* Dectivate BM unit */
  388. mvneta_bm_write(priv, MVNETA_BM_COMMAND_REG, MVNETA_BM_STOP_MASK);
  389. clk_disable_unprepare(priv->clk);
  390. return 0;
  391. }
  392. static const struct of_device_id mvneta_bm_match[] = {
  393. { .compatible = "marvell,armada-380-neta-bm" },
  394. { }
  395. };
  396. MODULE_DEVICE_TABLE(of, mvneta_bm_match);
  397. static struct platform_driver mvneta_bm_driver = {
  398. .probe = mvneta_bm_probe,
  399. .remove = mvneta_bm_remove,
  400. .driver = {
  401. .name = MVNETA_BM_DRIVER_NAME,
  402. .of_match_table = mvneta_bm_match,
  403. },
  404. };
  405. module_platform_driver(mvneta_bm_driver);
  406. MODULE_DESCRIPTION("Marvell NETA Buffer Manager Driver - www.marvell.com");
  407. MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
  408. MODULE_LICENSE("GPL v2");