mvneta_bm.c 13 KB

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