2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * Provides low-level access to the NFP's internal CPP bus
37 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38 * Jason McMullan <jason.mcmullan@netronome.com>
39 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
42 #include <asm/unaligned.h>
43 #include <linux/delay.h>
44 #include <linux/device.h>
45 #include <linux/ioport.h>
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/mutex.h>
49 #include <linux/sched.h>
50 #include <linux/slab.h>
51 #include <linux/wait.h>
55 #include "nfp6000/nfp6000.h"
57 #define NFP_ARM_GCSR_SOFTMODEL2 0x0000014c
58 #define NFP_ARM_GCSR_SOFTMODEL3 0x00000150
60 struct nfp_cpp_resource {
61 struct list_head list;
69 * struct nfp_cpp - main nfpcore device structure
70 * Following fields are read-only after probe() exits or netdevs are spawned.
71 * @dev: embedded device structure
72 * @op: low-level implementation ops
73 * @priv: private data of the low-level implementation
75 * @interface: chip interface id we are using to reach it
76 * @serial: chip serial number
77 * @imb_cat_table: CPP Mapping Table
79 * Following fields use explicit locking:
80 * @resource_list: NFP CPP resource list
81 * @resource_lock: protects @resource_list
83 * @area_cache_list: cached areas for cpp/xpb read/write speed up
84 * @area_cache_mutex: protects @area_cache_list
86 * @waitq: area wait queue
95 u8 serial[NFP_SERIAL_LEN];
97 const struct nfp_cpp_operations *op;
98 struct list_head resource_list;
99 rwlock_t resource_lock;
100 wait_queue_head_t waitq;
102 u32 imb_cat_table[16];
104 struct mutex area_cache_mutex;
105 struct list_head area_cache_list;
108 /* Element of the area_cache_list */
109 struct nfp_cpp_area_cache {
110 struct list_head entry;
114 struct nfp_cpp_area *area;
117 struct nfp_cpp_area {
121 struct mutex mutex; /* Lock for the area's refcount */
122 unsigned long long offset;
124 struct nfp_cpp_resource resource;
126 /* Here follows the 'priv' part of nfp_cpp_area. */
129 struct nfp_cpp_explicit {
131 struct nfp_cpp_explicit_command cmd;
132 /* Here follows the 'priv' part of nfp_cpp_area. */
135 static void __resource_add(struct list_head *head, struct nfp_cpp_resource *res)
137 struct nfp_cpp_resource *tmp;
138 struct list_head *pos;
140 list_for_each(pos, head) {
141 tmp = container_of(pos, struct nfp_cpp_resource, list);
143 if (tmp->cpp_id > res->cpp_id)
146 if (tmp->cpp_id == res->cpp_id && tmp->start > res->start)
150 list_add_tail(&res->list, pos);
153 static void __resource_del(struct nfp_cpp_resource *res)
155 list_del_init(&res->list);
158 static void __release_cpp_area(struct kref *kref)
160 struct nfp_cpp_area *area =
161 container_of(kref, struct nfp_cpp_area, kref);
162 struct nfp_cpp *cpp = nfp_cpp_area_cpp(area);
164 if (area->cpp->op->area_cleanup)
165 area->cpp->op->area_cleanup(area);
167 write_lock(&cpp->resource_lock);
168 __resource_del(&area->resource);
169 write_unlock(&cpp->resource_lock);
173 static void nfp_cpp_area_put(struct nfp_cpp_area *area)
175 kref_put(&area->kref, __release_cpp_area);
178 static struct nfp_cpp_area *nfp_cpp_area_get(struct nfp_cpp_area *area)
180 kref_get(&area->kref);
186 * nfp_cpp_free() - free the CPP handle
189 void nfp_cpp_free(struct nfp_cpp *cpp)
191 struct nfp_cpp_area_cache *cache, *ctmp;
192 struct nfp_cpp_resource *res, *rtmp;
194 /* Remove all caches */
195 list_for_each_entry_safe(cache, ctmp, &cpp->area_cache_list, entry) {
196 list_del(&cache->entry);
198 nfp_cpp_area_release(cache->area);
199 nfp_cpp_area_free(cache->area);
203 /* There should be no dangling areas at this point */
204 WARN_ON(!list_empty(&cpp->resource_list));
206 /* .. but if they weren't, try to clean up. */
207 list_for_each_entry_safe(res, rtmp, &cpp->resource_list, list) {
208 struct nfp_cpp_area *area = container_of(res,
212 dev_err(cpp->dev.parent, "Dangling area: %d:%d:%d:0x%0llx-0x%0llx%s%s\n",
213 NFP_CPP_ID_TARGET_of(res->cpp_id),
214 NFP_CPP_ID_ACTION_of(res->cpp_id),
215 NFP_CPP_ID_TOKEN_of(res->cpp_id),
216 res->start, res->end,
217 res->name ? " " : "",
218 res->name ? res->name : "");
220 if (area->cpp->op->area_release)
221 area->cpp->op->area_release(area);
223 __release_cpp_area(&area->kref);
229 device_unregister(&cpp->dev);
235 * nfp_cpp_model() - Retrieve the Model ID of the NFP
236 * @cpp: NFP CPP handle
238 * Return: NFP CPP Model ID
240 u32 nfp_cpp_model(struct nfp_cpp *cpp)
246 * nfp_cpp_interface() - Retrieve the Interface ID of the NFP
247 * @cpp: NFP CPP handle
249 * Return: NFP CPP Interface ID
251 u16 nfp_cpp_interface(struct nfp_cpp *cpp)
253 return cpp->interface;
257 * nfp_cpp_serial() - Retrieve the Serial ID of the NFP
258 * @cpp: NFP CPP handle
259 * @serial: Pointer to NFP serial number
261 * Return: Length of NFP serial number
263 int nfp_cpp_serial(struct nfp_cpp *cpp, const u8 **serial)
265 *serial = &cpp->serial[0];
266 return sizeof(cpp->serial);
270 * nfp_cpp_area_alloc_with_name() - allocate a new CPP area
271 * @cpp: CPP device handle
273 * @name: Name of region
274 * @address: Address of region
275 * @size: Size of region
277 * Allocate and initialize a CPP area structure. The area must later
278 * be locked down with an 'acquire' before it can be safely accessed.
280 * NOTE: @address and @size must be 32-bit aligned values.
282 * Return: NFP CPP area handle, or NULL
284 struct nfp_cpp_area *
285 nfp_cpp_area_alloc_with_name(struct nfp_cpp *cpp, u32 dest, const char *name,
286 unsigned long long address, unsigned long size)
288 struct nfp_cpp_area *area;
292 /* Remap from cpp_island to cpp_target */
293 err = nfp_target_cpp(dest, tmp64, &dest, &tmp64, cpp->imb_cat_table);
302 name_len = strlen(name) + 1;
303 area = kzalloc(sizeof(*area) + cpp->op->area_priv_size + name_len,
309 area->resource.name = (void *)area + sizeof(*area) +
310 cpp->op->area_priv_size;
311 memcpy((char *)area->resource.name, name, name_len);
313 area->resource.cpp_id = dest;
314 area->resource.start = address;
315 area->resource.end = area->resource.start + size - 1;
316 INIT_LIST_HEAD(&area->resource.list);
318 atomic_set(&area->refcount, 0);
319 kref_init(&area->kref);
320 mutex_init(&area->mutex);
322 if (cpp->op->area_init) {
325 err = cpp->op->area_init(area, dest, address, size);
332 write_lock(&cpp->resource_lock);
333 __resource_add(&cpp->resource_list, &area->resource);
334 write_unlock(&cpp->resource_lock);
336 area->offset = address;
343 * nfp_cpp_area_alloc() - allocate a new CPP area
346 * @address: Start address on CPP target
347 * @size: Size of area in bytes
349 * Allocate and initialize a CPP area structure. The area must later
350 * be locked down with an 'acquire' before it can be safely accessed.
352 * NOTE: @address and @size must be 32-bit aligned values.
354 * Return: NFP CPP Area handle, or NULL
356 struct nfp_cpp_area *
357 nfp_cpp_area_alloc(struct nfp_cpp *cpp, u32 dest,
358 unsigned long long address, unsigned long size)
360 return nfp_cpp_area_alloc_with_name(cpp, dest, NULL, address, size);
364 * nfp_cpp_area_alloc_acquire() - allocate a new CPP area and lock it down
366 * @name: Name of region
368 * @address: Start address on CPP target
369 * @size: Size of area
371 * Allocate and initialize a CPP area structure, and lock it down so
372 * that it can be accessed directly.
374 * NOTE: @address and @size must be 32-bit aligned values.
376 * NOTE: The area must also be 'released' when the structure is freed.
378 * Return: NFP CPP Area handle, or NULL
380 struct nfp_cpp_area *
381 nfp_cpp_area_alloc_acquire(struct nfp_cpp *cpp, const char *name, u32 dest,
382 unsigned long long address, unsigned long size)
384 struct nfp_cpp_area *area;
386 area = nfp_cpp_area_alloc_with_name(cpp, dest, name, address, size);
390 if (nfp_cpp_area_acquire(area)) {
391 nfp_cpp_area_free(area);
399 * nfp_cpp_area_free() - free up the CPP area
400 * @area: CPP area handle
402 * Frees up memory resources held by the CPP area.
404 void nfp_cpp_area_free(struct nfp_cpp_area *area)
406 if (atomic_read(&area->refcount))
407 nfp_warn(area->cpp, "Warning: freeing busy area\n");
408 nfp_cpp_area_put(area);
411 static bool nfp_cpp_area_acquire_try(struct nfp_cpp_area *area, int *status)
413 *status = area->cpp->op->area_acquire(area);
415 return *status != -EAGAIN;
418 static int __nfp_cpp_area_acquire(struct nfp_cpp_area *area)
422 if (atomic_inc_return(&area->refcount) > 1)
425 if (!area->cpp->op->area_acquire)
428 err = wait_event_interruptible(area->cpp->waitq,
429 nfp_cpp_area_acquire_try(area, &status));
433 nfp_warn(area->cpp, "Warning: area wait failed: %d\n", err);
434 atomic_dec(&area->refcount);
438 nfp_cpp_area_get(area);
444 * nfp_cpp_area_acquire() - lock down a CPP area for access
445 * @area: CPP area handle
447 * Locks down the CPP area for a potential long term activity. Area
448 * must always be locked down before being accessed.
450 * Return: 0, or -ERRNO
452 int nfp_cpp_area_acquire(struct nfp_cpp_area *area)
456 mutex_lock(&area->mutex);
457 ret = __nfp_cpp_area_acquire(area);
458 mutex_unlock(&area->mutex);
464 * nfp_cpp_area_acquire_nonblocking() - lock down a CPP area for access
465 * @area: CPP area handle
467 * Locks down the CPP area for a potential long term activity. Area
468 * must always be locked down before being accessed.
470 * NOTE: Returns -EAGAIN is no area is available
472 * Return: 0, or -ERRNO
474 int nfp_cpp_area_acquire_nonblocking(struct nfp_cpp_area *area)
476 mutex_lock(&area->mutex);
477 if (atomic_inc_return(&area->refcount) == 1) {
478 if (area->cpp->op->area_acquire) {
481 err = area->cpp->op->area_acquire(area);
483 atomic_dec(&area->refcount);
484 mutex_unlock(&area->mutex);
489 mutex_unlock(&area->mutex);
491 nfp_cpp_area_get(area);
496 * nfp_cpp_area_release() - release a locked down CPP area
497 * @area: CPP area handle
499 * Releases a previously locked down CPP area.
501 void nfp_cpp_area_release(struct nfp_cpp_area *area)
503 mutex_lock(&area->mutex);
504 /* Only call the release on refcount == 0 */
505 if (atomic_dec_and_test(&area->refcount)) {
506 if (area->cpp->op->area_release) {
507 area->cpp->op->area_release(area);
508 /* Let anyone waiting for a BAR try to get one.. */
509 wake_up_interruptible_all(&area->cpp->waitq);
512 mutex_unlock(&area->mutex);
514 nfp_cpp_area_put(area);
518 * nfp_cpp_area_release_free() - release CPP area and free it
519 * @area: CPP area handle
521 * Releases CPP area and frees up memory resources held by the it.
523 void nfp_cpp_area_release_free(struct nfp_cpp_area *area)
525 nfp_cpp_area_release(area);
526 nfp_cpp_area_free(area);
530 * nfp_cpp_area_read() - read data from CPP area
531 * @area: CPP area handle
532 * @offset: offset into CPP area
533 * @kernel_vaddr: kernel address to put data into
534 * @length: number of bytes to read
536 * Read data from indicated CPP region.
538 * NOTE: @offset and @length must be 32-bit aligned values.
540 * NOTE: Area must have been locked down with an 'acquire'.
542 * Return: length of io, or -ERRNO
544 int nfp_cpp_area_read(struct nfp_cpp_area *area,
545 unsigned long offset, void *kernel_vaddr,
548 return area->cpp->op->area_read(area, kernel_vaddr, offset, length);
552 * nfp_cpp_area_write() - write data to CPP area
553 * @area: CPP area handle
554 * @offset: offset into CPP area
555 * @kernel_vaddr: kernel address to read data from
556 * @length: number of bytes to write
558 * Write data to indicated CPP region.
560 * NOTE: @offset and @length must be 32-bit aligned values.
562 * NOTE: Area must have been locked down with an 'acquire'.
564 * Return: length of io, or -ERRNO
566 int nfp_cpp_area_write(struct nfp_cpp_area *area,
567 unsigned long offset, const void *kernel_vaddr,
570 return area->cpp->op->area_write(area, kernel_vaddr, offset, length);
574 * nfp_cpp_area_name() - return name of a CPP area
575 * @cpp_area: CPP area handle
577 * Return: Name of the area, or NULL
579 const char *nfp_cpp_area_name(struct nfp_cpp_area *cpp_area)
581 return cpp_area->resource.name;
585 * nfp_cpp_area_priv() - return private struct for CPP area
586 * @cpp_area: CPP area handle
588 * Return: Private data for the CPP area
590 void *nfp_cpp_area_priv(struct nfp_cpp_area *cpp_area)
596 * nfp_cpp_area_cpp() - return CPP handle for CPP area
597 * @cpp_area: CPP area handle
599 * Return: NFP CPP handle
601 struct nfp_cpp *nfp_cpp_area_cpp(struct nfp_cpp_area *cpp_area)
603 return cpp_area->cpp;
607 * nfp_cpp_area_resource() - get resource
608 * @area: CPP area handle
610 * NOTE: Area must have been locked down with an 'acquire'.
612 * Return: struct resource pointer, or NULL
614 struct resource *nfp_cpp_area_resource(struct nfp_cpp_area *area)
616 struct resource *res = NULL;
618 if (area->cpp->op->area_resource)
619 res = area->cpp->op->area_resource(area);
625 * nfp_cpp_area_phys() - get physical address of CPP area
626 * @area: CPP area handle
628 * NOTE: Area must have been locked down with an 'acquire'.
630 * Return: phy_addr_t of the area, or NULL
632 phys_addr_t nfp_cpp_area_phys(struct nfp_cpp_area *area)
634 phys_addr_t addr = ~0;
636 if (area->cpp->op->area_phys)
637 addr = area->cpp->op->area_phys(area);
643 * nfp_cpp_area_iomem() - get IOMEM region for CPP area
644 * @area: CPP area handle
646 * Returns an iomem pointer for use with readl()/writel() style
649 * NOTE: Area must have been locked down with an 'acquire'.
651 * Return: __iomem pointer to the area, or NULL
653 void __iomem *nfp_cpp_area_iomem(struct nfp_cpp_area *area)
655 void __iomem *iomem = NULL;
657 if (area->cpp->op->area_iomem)
658 iomem = area->cpp->op->area_iomem(area);
664 * nfp_cpp_area_readl() - Read a u32 word from an area
665 * @area: CPP Area handle
666 * @offset: Offset into area
667 * @value: Pointer to read buffer
669 * Return: length of the io, or -ERRNO
671 int nfp_cpp_area_readl(struct nfp_cpp_area *area,
672 unsigned long offset, u32 *value)
677 err = nfp_cpp_area_read(area, offset, &tmp, sizeof(tmp));
678 *value = get_unaligned_le32(tmp);
684 * nfp_cpp_area_writel() - Write a u32 word to an area
685 * @area: CPP Area handle
686 * @offset: Offset into area
687 * @value: Value to write
689 * Return: length of the io, or -ERRNO
691 int nfp_cpp_area_writel(struct nfp_cpp_area *area,
692 unsigned long offset, u32 value)
696 put_unaligned_le32(value, tmp);
698 return nfp_cpp_area_write(area, offset, &tmp, sizeof(tmp));
702 * nfp_cpp_area_readq() - Read a u64 word from an area
703 * @area: CPP Area handle
704 * @offset: Offset into area
705 * @value: Pointer to read buffer
707 * Return: length of the io, or -ERRNO
709 int nfp_cpp_area_readq(struct nfp_cpp_area *area,
710 unsigned long offset, u64 *value)
715 err = nfp_cpp_area_read(area, offset, &tmp, sizeof(tmp));
716 *value = get_unaligned_le64(tmp);
722 * nfp_cpp_area_writeq() - Write a u64 word to an area
723 * @area: CPP Area handle
724 * @offset: Offset into area
725 * @value: Value to write
727 * Return: length of the io, or -ERRNO
729 int nfp_cpp_area_writeq(struct nfp_cpp_area *area,
730 unsigned long offset, u64 value)
734 put_unaligned_le64(value, tmp);
736 return nfp_cpp_area_write(area, offset, &tmp, sizeof(tmp));
740 * nfp_cpp_area_fill() - fill a CPP area with a value
742 * @offset: offset into CPP area
743 * @value: value to fill with
744 * @length: length of area to fill
746 * Fill indicated area with given value.
748 * Return: length of io, or -ERRNO
750 int nfp_cpp_area_fill(struct nfp_cpp_area *area,
751 unsigned long offset, u32 value, size_t length)
757 put_unaligned_le32(value, tmp);
759 if (offset % sizeof(tmp) || length % sizeof(tmp))
762 for (i = 0; i < length; i += sizeof(tmp)) {
763 k = nfp_cpp_area_write(area, offset + i, &tmp, sizeof(tmp));
772 * nfp_cpp_area_cache_add() - Permanently reserve and area for the hot cache
773 * @cpp: NFP CPP handle
774 * @size: Size of the area - MUST BE A POWER OF 2.
776 int nfp_cpp_area_cache_add(struct nfp_cpp *cpp, size_t size)
778 struct nfp_cpp_area_cache *cache;
779 struct nfp_cpp_area *area;
781 /* Allocate an area - we use the MU target's base as a placeholder,
782 * as all supported chips have a MU.
784 area = nfp_cpp_area_alloc(cpp, NFP_CPP_ID(7, NFP_CPP_ACTION_RW, 0),
789 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
791 nfp_cpp_area_free(area);
799 mutex_lock(&cpp->area_cache_mutex);
800 list_add_tail(&cache->entry, &cpp->area_cache_list);
801 mutex_unlock(&cpp->area_cache_mutex);
806 static struct nfp_cpp_area_cache *
807 area_cache_get(struct nfp_cpp *cpp, u32 id,
808 u64 addr, unsigned long *offset, size_t length)
810 struct nfp_cpp_area_cache *cache;
813 /* Early exit when length == 0, which prevents
814 * the need for special case code below when
815 * checking against available cache size.
817 if (length == 0 || id == 0)
820 /* Remap from cpp_island to cpp_target */
821 err = nfp_target_cpp(id, addr, &id, &addr, cpp->imb_cat_table);
825 mutex_lock(&cpp->area_cache_mutex);
827 if (list_empty(&cpp->area_cache_list)) {
828 mutex_unlock(&cpp->area_cache_mutex);
834 /* See if we have a match */
835 list_for_each_entry(cache, &cpp->area_cache_list, entry) {
836 if (id == cache->id &&
837 addr >= cache->addr &&
838 addr + length <= cache->addr + cache->size)
842 /* No matches - inspect the tail of the LRU */
843 cache = list_entry(cpp->area_cache_list.prev,
844 struct nfp_cpp_area_cache, entry);
846 /* Can we fit in the cache entry? */
847 if (round_down(addr + length - 1, cache->size) !=
848 round_down(addr, cache->size)) {
849 mutex_unlock(&cpp->area_cache_mutex);
853 /* If id != 0, we will need to release it */
855 nfp_cpp_area_release(cache->area);
860 /* Adjust the start address to be cache size aligned */
862 cache->addr = addr & ~(u64)(cache->size - 1);
864 /* Re-init to the new ID and address */
865 if (cpp->op->area_init) {
866 err = cpp->op->area_init(cache->area,
867 id, cache->addr, cache->size);
869 mutex_unlock(&cpp->area_cache_mutex);
874 /* Attempt to acquire */
875 err = nfp_cpp_area_acquire(cache->area);
877 mutex_unlock(&cpp->area_cache_mutex);
883 *offset = addr - cache->addr;
888 area_cache_put(struct nfp_cpp *cpp, struct nfp_cpp_area_cache *cache)
893 /* Move to front of LRU */
894 list_del(&cache->entry);
895 list_add(&cache->entry, &cpp->area_cache_list);
897 mutex_unlock(&cpp->area_cache_mutex);
900 static int __nfp_cpp_read(struct nfp_cpp *cpp, u32 destination,
901 unsigned long long address, void *kernel_vaddr,
904 struct nfp_cpp_area_cache *cache;
905 struct nfp_cpp_area *area;
906 unsigned long offset = 0;
909 cache = area_cache_get(cpp, destination, address, &offset, length);
913 area = nfp_cpp_area_alloc(cpp, destination, address, length);
917 err = nfp_cpp_area_acquire(area);
919 nfp_cpp_area_free(area);
924 err = nfp_cpp_area_read(area, offset, kernel_vaddr, length);
927 area_cache_put(cpp, cache);
929 nfp_cpp_area_release_free(area);
935 * nfp_cpp_read() - read from CPP target
937 * @destination: CPP id
938 * @address: offset into CPP target
939 * @kernel_vaddr: kernel buffer for result
940 * @length: number of bytes to read
942 * Return: length of io, or -ERRNO
944 int nfp_cpp_read(struct nfp_cpp *cpp, u32 destination,
945 unsigned long long address, void *kernel_vaddr,
951 for (offset = 0; offset < length; offset += n) {
952 unsigned long long r_addr = address + offset;
954 /* make first read smaller to align to safe window */
955 n = min_t(size_t, length - offset,
956 ALIGN(r_addr + 1, NFP_CPP_SAFE_AREA_SIZE) - r_addr);
958 ret = __nfp_cpp_read(cpp, destination, address + offset,
959 kernel_vaddr + offset, n);
969 static int __nfp_cpp_write(struct nfp_cpp *cpp, u32 destination,
970 unsigned long long address,
971 const void *kernel_vaddr, size_t length)
973 struct nfp_cpp_area_cache *cache;
974 struct nfp_cpp_area *area;
975 unsigned long offset = 0;
978 cache = area_cache_get(cpp, destination, address, &offset, length);
982 area = nfp_cpp_area_alloc(cpp, destination, address, length);
986 err = nfp_cpp_area_acquire(area);
988 nfp_cpp_area_free(area);
993 err = nfp_cpp_area_write(area, offset, kernel_vaddr, length);
996 area_cache_put(cpp, cache);
998 nfp_cpp_area_release_free(area);
1004 * nfp_cpp_write() - write to CPP target
1006 * @destination: CPP id
1007 * @address: offset into CPP target
1008 * @kernel_vaddr: kernel buffer to read from
1009 * @length: number of bytes to write
1011 * Return: length of io, or -ERRNO
1013 int nfp_cpp_write(struct nfp_cpp *cpp, u32 destination,
1014 unsigned long long address,
1015 const void *kernel_vaddr, size_t length)
1020 for (offset = 0; offset < length; offset += n) {
1021 unsigned long long w_addr = address + offset;
1023 /* make first write smaller to align to safe window */
1024 n = min_t(size_t, length - offset,
1025 ALIGN(w_addr + 1, NFP_CPP_SAFE_AREA_SIZE) - w_addr);
1027 ret = __nfp_cpp_write(cpp, destination, address + offset,
1028 kernel_vaddr + offset, n);
1038 /* Return the correct CPP address, and fixup xpb_addr as needed. */
1039 static u32 nfp_xpb_to_cpp(struct nfp_cpp *cpp, u32 *xpb_addr)
1044 xpb = NFP_CPP_ID(14, NFP_CPP_ACTION_RW, 0);
1045 /* Ensure that non-local XPB accesses go
1046 * out through the global XPBM bus.
1048 island = (*xpb_addr >> 24) & 0x3f;
1053 *xpb_addr |= 1 << 30;
1057 /* Accesses to the ARM Island overlay uses Island 0 / Global Bit */
1058 *xpb_addr &= ~0x7f000000;
1059 if (*xpb_addr < 0x60000) {
1060 *xpb_addr |= 1 << 30;
1062 /* And only non-ARM interfaces use the island id = 1 */
1063 if (NFP_CPP_INTERFACE_TYPE_of(nfp_cpp_interface(cpp))
1064 != NFP_CPP_INTERFACE_TYPE_ARM)
1065 *xpb_addr |= 1 << 24;
1072 * nfp_xpb_readl() - Read a u32 word from a XPB location
1073 * @cpp: CPP device handle
1074 * @xpb_addr: Address for operation
1075 * @value: Pointer to read buffer
1077 * Return: length of the io, or -ERRNO
1079 int nfp_xpb_readl(struct nfp_cpp *cpp, u32 xpb_addr, u32 *value)
1081 u32 cpp_dest = nfp_xpb_to_cpp(cpp, &xpb_addr);
1083 return nfp_cpp_readl(cpp, cpp_dest, xpb_addr, value);
1087 * nfp_xpb_writel() - Write a u32 word to a XPB location
1088 * @cpp: CPP device handle
1089 * @xpb_addr: Address for operation
1090 * @value: Value to write
1092 * Return: length of the io, or -ERRNO
1094 int nfp_xpb_writel(struct nfp_cpp *cpp, u32 xpb_addr, u32 value)
1096 u32 cpp_dest = nfp_xpb_to_cpp(cpp, &xpb_addr);
1098 return nfp_cpp_writel(cpp, cpp_dest, xpb_addr, value);
1102 * nfp_xpb_writelm() - Modify bits of a 32-bit value from the XPB bus
1103 * @cpp: NFP CPP device handle
1104 * @xpb_tgt: XPB target and address
1105 * @mask: mask of bits to alter
1106 * @value: value to modify
1108 * KERNEL: This operation is safe to call in interrupt or softirq context.
1110 * Return: length of the io, or -ERRNO
1112 int nfp_xpb_writelm(struct nfp_cpp *cpp, u32 xpb_tgt,
1113 u32 mask, u32 value)
1118 err = nfp_xpb_readl(cpp, xpb_tgt, &tmp);
1123 tmp |= mask & value;
1124 return nfp_xpb_writel(cpp, xpb_tgt, tmp);
1127 /* Lockdep markers */
1128 static struct lock_class_key nfp_cpp_resource_lock_key;
1130 static void nfp_cpp_dev_release(struct device *dev)
1132 /* Nothing to do here - it just makes the kernel happy */
1136 * nfp_cpp_from_operations() - Create a NFP CPP handle
1137 * from an operations structure
1138 * @ops: NFP CPP operations structure
1139 * @parent: Parent device
1140 * @priv: Private data of low-level implementation
1142 * NOTE: On failure, cpp_ops->free will be called!
1144 * Return: NFP CPP handle on success, ERR_PTR on failure
1147 nfp_cpp_from_operations(const struct nfp_cpp_operations *ops,
1148 struct device *parent, void *priv)
1150 const u32 arm = NFP_CPP_ID(NFP_CPP_TARGET_ARM, NFP_CPP_ACTION_RW, 0);
1151 struct nfp_cpp *cpp;
1157 cpp = kzalloc(sizeof(*cpp), GFP_KERNEL);
1165 cpp->interface = ops->get_interface(parent);
1166 if (ops->read_serial)
1167 ops->read_serial(parent, cpp->serial);
1168 rwlock_init(&cpp->resource_lock);
1169 init_waitqueue_head(&cpp->waitq);
1170 lockdep_set_class(&cpp->resource_lock, &nfp_cpp_resource_lock_key);
1171 INIT_LIST_HEAD(&cpp->resource_list);
1172 INIT_LIST_HEAD(&cpp->area_cache_list);
1173 mutex_init(&cpp->area_cache_mutex);
1174 cpp->dev.init_name = "cpp";
1175 cpp->dev.parent = parent;
1176 cpp->dev.release = nfp_cpp_dev_release;
1177 err = device_register(&cpp->dev);
1179 put_device(&cpp->dev);
1183 dev_set_drvdata(&cpp->dev, cpp);
1185 /* NOTE: cpp_lock is NOT locked for op->init,
1186 * since it may call NFP CPP API operations
1188 if (cpp->op->init) {
1189 err = cpp->op->init(cpp);
1192 "NFP interface initialization failed\n");
1197 err = nfp_cpp_model_autodetect(cpp, &cpp->model);
1199 dev_err(parent, "NFP model detection failed\n");
1203 for (tgt = 0; tgt < ARRAY_SIZE(cpp->imb_cat_table); tgt++) {
1204 /* Hardcoded XPB IMB Base, island 0 */
1205 xpbaddr = 0x000a0000 + (tgt * 4);
1206 err = nfp_xpb_readl(cpp, xpbaddr,
1207 &cpp->imb_cat_table[tgt]);
1210 "Can't read CPP mapping from device\n");
1215 nfp_cpp_readl(cpp, arm, NFP_ARM_GCSR + NFP_ARM_GCSR_SOFTMODEL2,
1217 nfp_cpp_readl(cpp, arm, NFP_ARM_GCSR + NFP_ARM_GCSR_SOFTMODEL3,
1220 dev_info(cpp->dev.parent, "Model: 0x%08x, SN: %pM, Ifc: 0x%04x\n",
1221 nfp_cpp_model(cpp), cpp->serial, nfp_cpp_interface(cpp));
1226 device_unregister(&cpp->dev);
1230 return ERR_PTR(err);
1234 * nfp_cpp_priv() - Get the operations private data of a CPP handle
1237 * Return: Private data for the NFP CPP handle
1239 void *nfp_cpp_priv(struct nfp_cpp *cpp)
1245 * nfp_cpp_device() - Get the Linux device handle of a CPP handle
1248 * Return: Device for the NFP CPP bus
1250 struct device *nfp_cpp_device(struct nfp_cpp *cpp)
1255 #define NFP_EXPL_OP(func, expl, args...) \
1257 struct nfp_cpp *cpp = nfp_cpp_explicit_cpp(expl); \
1258 int err = -ENODEV; \
1260 if (cpp->op->func) \
1261 err = cpp->op->func(expl, ##args); \
1265 #define NFP_EXPL_OP_NR(func, expl, args...) \
1267 struct nfp_cpp *cpp = nfp_cpp_explicit_cpp(expl); \
1269 if (cpp->op->func) \
1270 cpp->op->func(expl, ##args); \
1275 * nfp_cpp_explicit_acquire() - Acquire explicit access handle
1276 * @cpp: NFP CPP handle
1278 * The 'data_ref' and 'signal_ref' values are useful when
1279 * constructing the NFP_EXPL_CSR1 and NFP_EXPL_POST values.
1281 * Return: NFP CPP explicit handle
1283 struct nfp_cpp_explicit *nfp_cpp_explicit_acquire(struct nfp_cpp *cpp)
1285 struct nfp_cpp_explicit *expl;
1288 expl = kzalloc(sizeof(*expl) + cpp->op->explicit_priv_size, GFP_KERNEL);
1293 err = NFP_EXPL_OP(explicit_acquire, expl);
1303 * nfp_cpp_explicit_set_target() - Set target fields for explicit
1304 * @expl: Explicit handle
1305 * @cpp_id: CPP ID field
1306 * @len: CPP Length field
1307 * @mask: CPP Mask field
1309 * Return: 0, or -ERRNO
1311 int nfp_cpp_explicit_set_target(struct nfp_cpp_explicit *expl,
1312 u32 cpp_id, u8 len, u8 mask)
1314 expl->cmd.cpp_id = cpp_id;
1315 expl->cmd.len = len;
1316 expl->cmd.byte_mask = mask;
1322 * nfp_cpp_explicit_set_data() - Set data fields for explicit
1323 * @expl: Explicit handle
1324 * @data_master: CPP Data Master field
1325 * @data_ref: CPP Data Ref field
1327 * Return: 0, or -ERRNO
1329 int nfp_cpp_explicit_set_data(struct nfp_cpp_explicit *expl,
1330 u8 data_master, u16 data_ref)
1332 expl->cmd.data_master = data_master;
1333 expl->cmd.data_ref = data_ref;
1339 * nfp_cpp_explicit_set_signal() - Set signal fields for explicit
1340 * @expl: Explicit handle
1341 * @signal_master: CPP Signal Master field
1342 * @signal_ref: CPP Signal Ref field
1344 * Return: 0, or -ERRNO
1346 int nfp_cpp_explicit_set_signal(struct nfp_cpp_explicit *expl,
1347 u8 signal_master, u8 signal_ref)
1349 expl->cmd.signal_master = signal_master;
1350 expl->cmd.signal_ref = signal_ref;
1356 * nfp_cpp_explicit_set_posted() - Set completion fields for explicit
1357 * @expl: Explicit handle
1358 * @posted: True for signaled completion, false otherwise
1359 * @siga: CPP Signal A field
1360 * @siga_mode: CPP Signal A Mode field
1361 * @sigb: CPP Signal B field
1362 * @sigb_mode: CPP Signal B Mode field
1364 * Return: 0, or -ERRNO
1366 int nfp_cpp_explicit_set_posted(struct nfp_cpp_explicit *expl, int posted,
1368 enum nfp_cpp_explicit_signal_mode siga_mode,
1370 enum nfp_cpp_explicit_signal_mode sigb_mode)
1372 expl->cmd.posted = posted;
1373 expl->cmd.siga = siga;
1374 expl->cmd.sigb = sigb;
1375 expl->cmd.siga_mode = siga_mode;
1376 expl->cmd.sigb_mode = sigb_mode;
1382 * nfp_cpp_explicit_put() - Set up the write (pull) data for a explicit access
1383 * @expl: NFP CPP Explicit handle
1384 * @buff: Data to have the target pull in the transaction
1385 * @len: Length of data, in bytes
1387 * The 'len' parameter must be less than or equal to 128 bytes.
1389 * If this function is called before the configuration
1390 * registers are set, it will return -EINVAL.
1392 * Return: 0, or -ERRNO
1394 int nfp_cpp_explicit_put(struct nfp_cpp_explicit *expl,
1395 const void *buff, size_t len)
1397 return NFP_EXPL_OP(explicit_put, expl, buff, len);
1401 * nfp_cpp_explicit_do() - Execute a transaction, and wait for it to complete
1402 * @expl: NFP CPP Explicit handle
1403 * @address: Address to send in the explicit transaction
1405 * If this function is called before the configuration
1406 * registers are set, it will return -1, with an errno of EINVAL.
1408 * Return: 0, or -ERRNO
1410 int nfp_cpp_explicit_do(struct nfp_cpp_explicit *expl, u64 address)
1412 return NFP_EXPL_OP(explicit_do, expl, &expl->cmd, address);
1416 * nfp_cpp_explicit_get() - Get the 'push' (read) data from a explicit access
1417 * @expl: NFP CPP Explicit handle
1418 * @buff: Data that the target pushed in the transaction
1419 * @len: Length of data, in bytes
1421 * The 'len' parameter must be less than or equal to 128 bytes.
1423 * If this function is called before all three configuration
1424 * registers are set, it will return -1, with an errno of EINVAL.
1426 * If this function is called before nfp_cpp_explicit_do()
1427 * has completed, it will return -1, with an errno of EBUSY.
1429 * Return: 0, or -ERRNO
1431 int nfp_cpp_explicit_get(struct nfp_cpp_explicit *expl, void *buff, size_t len)
1433 return NFP_EXPL_OP(explicit_get, expl, buff, len);
1437 * nfp_cpp_explicit_release() - Release explicit access handle
1438 * @expl: NFP CPP Explicit handle
1441 void nfp_cpp_explicit_release(struct nfp_cpp_explicit *expl)
1443 NFP_EXPL_OP_NR(explicit_release, expl);
1448 * nfp_cpp_explicit_cpp() - return CPP handle for CPP explicit
1449 * @cpp_explicit: CPP explicit handle
1451 * Return: NFP CPP handle of the explicit
1453 struct nfp_cpp *nfp_cpp_explicit_cpp(struct nfp_cpp_explicit *cpp_explicit)
1455 return cpp_explicit->cpp;
1459 * nfp_cpp_explicit_priv() - return private struct for CPP explicit
1460 * @cpp_explicit: CPP explicit handle
1462 * Return: private data of the explicit, or NULL
1464 void *nfp_cpp_explicit_priv(struct nfp_cpp_explicit *cpp_explicit)
1466 return &cpp_explicit[1];