linux/drivers/misc/xilinx-ai-engine/ai-engine-internal.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0-only */
   2/*
   3 * Xilinx AI Engine driver internal header
   4 *
   5 * Copyright (C) 2020 - 2021 Xilinx, Inc.
   6 */
   7
   8#ifndef AIE_INTERNAL_H
   9#define AIE_INTERNAL_H
  10
  11#include <linux/bitfield.h>
  12#include <linux/bitmap.h>
  13#include <linux/bits.h>
  14#include <linux/cdev.h>
  15#include <linux/clk.h>
  16#include <linux/device.h>
  17#include <linux/dma-buf.h>
  18#include <linux/file.h>
  19#include <linux/fpga/fpga-bridge.h>
  20#include <linux/io.h>
  21#include <linux/interrupt.h>
  22#include <linux/list.h>
  23#include <linux/mutex.h>
  24#include <linux/of.h>
  25#include <linux/of_device.h>
  26#include <linux/slab.h>
  27#include <uapi/linux/xlnx-ai-engine.h>
  28
  29/*
  30 * Macros for AI engine tile type bitmasks
  31 */
  32enum aie_tile_type {
  33        AIE_TILE_TYPE_TILE,
  34        AIE_TILE_TYPE_SHIMPL,
  35        AIE_TILE_TYPE_SHIMNOC,
  36        AIE_TILE_TYPE_MAX
  37};
  38
  39#define AIE_TILE_TYPE_MASK_TILE         BIT(AIE_TILE_TYPE_TILE)
  40#define AIE_TILE_TYPE_MASK_SHIMPL       BIT(AIE_TILE_TYPE_SHIMPL)
  41/* SHIM NOC tile includes SHIM PL and SHIM NOC modules */
  42#define AIE_TILE_TYPE_MASK_SHIMNOC      BIT(AIE_TILE_TYPE_SHIMNOC)
  43
  44/*
  45 * Macros for attribute property of AI engine registers accessed by kernel
  46 * 0 - 7 bits: tile type bits
  47 * 8 - 15 bits: permission bits. If it is 1, it allows write from userspace
  48 */
  49#define AIE_REGS_ATTR_TILE_TYPE_SHIFT   0U
  50#define AIE_REGS_ATTR_PERM_SHIFT        8U
  51#define AIE_REGS_ATTR_TILE_TYPE_MASK    GENMASK(AIE_REGS_ATTR_PERM_SHIFT - 1, \
  52                                                AIE_REGS_ATTR_TILE_TYPE_SHIFT)
  53#define AIE_REGS_ATTR_PERM_MASK         GENMASK(15, \
  54                                                AIE_REGS_ATTR_PERM_SHIFT)
  55
  56#define AIE_PART_STATUS_BRIDGE_DISABLED 0x1U
  57
  58/* Silicon Engineering Sample(ES) revision ID */
  59#define VERSAL_ES1_REV_ID               0x0
  60#define VERSAL_ES2_REV_ID               0x1
  61
  62#define AIE_NPI_ERROR_ID                BIT(1)
  63
  64/* Macros relevant to interrupts */
  65#define AIE_INTR_L2_CTRL_MASK_WIDTH     32
  66
  67/* Max number of modules per tile */
  68#define AIE_MAX_MODS_PER_TILE           2U
  69
  70/* AIE core registers step size */
  71#define AIE_CORE_REGS_STEP              0x10
  72
  73/*
  74 * Macros of AI engine module type index of a tile type
  75 * e.g.
  76 * id 0 of CORE tile is memory module, and 1 is core module
  77 * id 0 of SHIM tile is pl module, and 1 is noc module
  78 */
  79#define AIE_TILE_MOD_START              AIE_MEM_MOD
  80#define AIE_MOD_ID(T, M)                ((M) - AIE_##T ## _MOD_START)
  81#define AIE_TILE_MEM_MOD_ID             AIE_MOD_ID(TILE, AIE_MEM_MOD)
  82#define AIE_TILE_CORE_MOD_ID            AIE_MOD_ID(TILE, AIE_CORE_MOD)
  83#define AIE_SHIMPL_MOD_START            AIE_PL_MOD
  84#define AIE_SHIMNOC_MOD_START           AIE_PL_MOD
  85#define AIE_SHIM_PL_MOD_ID              AIE_MOD_ID(SHIMPL, AIE_PL_MOD)
  86#define AIE_SHIM_NOC_MOD_ID             AIE_MOD_ID(SHIMNOC, AIE_NOC_MOD)
  87
  88/* String delimiter to format sysfs data */
  89#define DELIMITER_LEVEL0 "|"
  90#define DELIMITER_LEVEL1 ", "
  91#define DELIMITER_LEVEL2 "; "
  92
  93/* Macros to define size of temporary string buffers */
  94#define AIE_SYSFS_CORE_STS_SIZE         100U
  95#define AIE_SYSFS_CHAN_STS_SIZE         150U
  96#define AIE_SYSFS_QUEUE_SIZE_SIZE       40U
  97#define AIE_SYSFS_QUEUE_STS_SIZE        60U
  98#define AIE_SYSFS_BD_SIZE               40U
  99#define AIE_SYSFS_ERROR_SIZE            300U
 100#define AIE_SYSFS_ERROR_CATEGORY_SIZE   500U
 101#define AIE_SYSFS_LOCK_STS_SIZE         400U
 102#define AIE_SYSFS_EVENT_STS_SIZE        550U
 103
 104/* Helper macros to dynamically create sysfs device attribute */
 105#define AIE_PART_DEV_ATTR_RO(_name) {                           \
 106        .name           = __stringify(_name),                   \
 107        .mode           = 0444,                                 \
 108        .show           = aie_part_show_##_name,                \
 109}
 110
 111#define AIE_PART_DEV_ATTR_WO(_name) {                           \
 112        .name           = __stringify(_name),                   \
 113        .mode           = 0200,                                 \
 114        .store          = aie_part_store_##_name,               \
 115}
 116
 117#define AIE_PART_DEV_ATTR_RW(_name) {                           \
 118        .name           = __stringify(_name),                   \
 119        .mode           = 0644,                                 \
 120        .show           = aie_part_show_##_name,                \
 121        .store          = aie_part_store_##_name,               \
 122}
 123
 124#define AIE_TILE_DEV_ATTR_RO(_name, _ttype) {                   \
 125        .name           = __stringify(_name),                   \
 126        .mode           = 0444,                                 \
 127        .tile_type      = _ttype,                               \
 128        .show           = aie_tile_show_##_name,                \
 129}
 130
 131#define AIE_TILE_DEV_ATTR_WO(_name, _ttype) {                   \
 132        .name           = __stringify(_name),                   \
 133        .mode           = 0200,                                 \
 134        .tile_type      = _ttype,                               \
 135        .store          = aie_tile_store_##_name,               \
 136}
 137
 138#define AIE_TILE_DEV_ATTR_RW(_name, _ttype) {                   \
 139        .name           = __stringify(_name),                   \
 140        .mode           = 0644,                                 \
 141        .tile_type      = _ttype,                               \
 142        .show           = aie_tile_show_##_name,                \
 143        .store          = aie_tile_store_##_name,               \
 144}
 145
 146#define AIE_PART_BIN_ATTR_RO(_name, _size) {                    \
 147        .name           = __stringify(_name),                   \
 148        .mode           = 0444,                                 \
 149        .size           = _size,                                \
 150        .read           = aie_sysfs_read_handler,               \
 151        .read_callback  = aie_part_read_cb_##_name,             \
 152}
 153
 154#define AIE_PART_BIN_ATTR_WO(_name, _size) {                    \
 155        .name           = __stringify(_name),                   \
 156        .mode           = 0200,                                 \
 157        .size           = _size,                                \
 158        .write          = aie_part_write_handler,               \
 159        .write_callback = aie_part_write_cb_##_name,            \
 160}
 161
 162#define AIE_PART_BIN_ATTR_RW(_name, _size) {                    \
 163        .name           = __stringify(_name),                   \
 164        .mode           = 0644                                  \
 165        .size           = _size,                                \
 166        .read           = aie_sysfs_read_handler,               \
 167        .write          = aie_part_write_handler,               \
 168        .read_callback  = aie_part_read_cb_##_name,             \
 169        .write_callback = aie_part_write_cb_##_name,            \
 170}
 171
 172#define AIE_TILE_BIN_ATTR_RO(_name, _size, _ttype) {            \
 173        .name           = __stringify(_name),                   \
 174        .mode           = 0444,                                 \
 175        .size           = _size,                                \
 176        .tile_type      = _ttype,                               \
 177        .read           = aie_sysfs_read_handler,               \
 178        .read_callback  = aie_tile_read_cb_##_name,             \
 179}
 180
 181#define AIE_TILE_BIN_ATTR_WO(_name, _size, _ttype) {            \
 182        .name           = __stringify(_name),                   \
 183        .mode           = 0200,                                 \
 184        .size           = _size,                                \
 185        .tile_type      = _ttype,                               \
 186        .write          = aie_tile_write_handler,               \
 187        .write_callback = aie_tile_write_cb_##_name,            \
 188}
 189
 190#define AIE_TILE_BIN_ATTR_RW(_name, _size, _ttype) {            \
 191        .name           = __stringify(_name),                   \
 192        .mode           = 0644,                                 \
 193        .size           = _size,                                \
 194        .tile_type      = _ttype,                               \
 195        .read           = aie_sysfs_read_handler,               \
 196        .write          = aie_tile_write_handler,               \
 197        .read_callback  = aie_tile_read_cb_##_name,             \
 198        .write_callback = aie_tile_write_cb_##_name,            \
 199}
 200
 201/*
 202 * enum aie_shim_switch_type - identifies different switches in shim tile.
 203 */
 204enum aie_shim_switch_type {
 205        AIE_SHIM_SWITCH_A,
 206        AIE_SHIM_SWITCH_B
 207};
 208
 209/**
 210 * struct aie_tile_regs - contiguous range of AI engine register
 211 *                        within an AI engine tile
 212 * @soff: start offset of the range
 213 * @eoff: end offset of the range
 214 * @attribute: registers attribute. It uses AIE_REGS_ATTR_* macros defined
 215 *             above.
 216 */
 217struct aie_tile_regs {
 218        size_t soff;
 219        size_t eoff;
 220        u32 attribute;
 221};
 222
 223/**
 224 * struct aie_single_reg_field - AI engine single field register attribute
 225 * @mask: field mask
 226 * @regoff: register offset of the field
 227 */
 228struct aie_single_reg_field {
 229        u32 mask;
 230        u32 regoff;
 231};
 232
 233struct aie_device;
 234struct aie_partition;
 235
 236/**
 237 * struct aie_part_mem - AI engine partition memory information structure
 238 * @apart: AI engine partition
 239 * @dbuf: dmabuf pointer associated with the memory
 240 * @mem: memory information of a type of memory
 241 * @size: size of the total memories in the partition
 242 *
 243 * This structure is to keep the information of a type of memory in a
 244 * partition. The memory information will be stored in @mem property.
 245 * The following information will be keep:
 246 *  * memory start address offset within a tile
 247 *  * memory size
 248 *  * what tiles contain this type of memory
 249 */
 250struct aie_part_mem {
 251        struct aie_partition *apart;
 252        struct dma_buf *dbuf;
 253        struct aie_mem mem;
 254        size_t size;
 255};
 256
 257/**
 258 * struct aie_dma_attr - AI engine DMA attributes structure
 259 * @laddr: low address field attributes
 260 * @haddr: high address field attributes
 261 * @buflen: buffer length field attributes
 262 * @sts: channel status field attributes
 263 * @stall: queue stall status field attributes
 264 * @qsize: queue size field attributes
 265 * @curbd: current buffer descriptor field attributes
 266 * @qsts: queue status field attributes
 267 * @bd_regoff: SHIM DMA buffer descriptors register offset
 268 * @mm2s_sts_regoff: MM2S status register offset
 269 * @s2mm_sts_regoff: S2MM status register offset
 270 * @num_mm2s_chan: number of MM2S channels
 271 * @num_s2mm_chan: number of S2MM channels
 272 * @num_bds: number of buffer descriptors
 273 * @bd_len: length of a buffer descriptor in bytes
 274 */
 275struct aie_dma_attr {
 276        struct aie_single_reg_field laddr;
 277        struct aie_single_reg_field haddr;
 278        struct aie_single_reg_field buflen;
 279        struct aie_single_reg_field sts;
 280        struct aie_single_reg_field stall;
 281        struct aie_single_reg_field qsize;
 282        struct aie_single_reg_field curbd;
 283        struct aie_single_reg_field qsts;
 284        u32 bd_regoff;
 285        u32 mm2s_sts_regoff;
 286        u32 s2mm_sts_regoff;
 287        u32 num_mm2s_chan;
 288        u32 num_s2mm_chan;
 289        u32 num_bds;
 290        u32 bd_len;
 291};
 292
 293/**
 294 * struct aie_core_regs_attr - AI engine core register attributes structure
 295 * @core_regs: core registers
 296 * @width: number of 32 bit words
 297 */
 298struct aie_core_regs_attr {
 299        const struct aie_tile_regs *core_regs;
 300        u32 width;
 301};
 302
 303/**
 304 * struct aie_tile_operations - AI engine device operations
 305 * @get_tile_type: get type of tile based on tile operation
 306 * @get_mem_info: get different types of memories information
 307 * @get_core_status: get the status of AIE core.
 308 * @reset_shim: reset shim, it will assert and then release SHIM reset
 309 * @init_part_clk_state: initialize clock states software structure which is a
 310 *                       bitmap for the AI engine partition. The clock states
 311 *                       structure is the structure used to keep track of if
 312 *                       the modules in the AI engine partition are gated.
 313 * @scan_part_clocks: scan partition modules to check whether the modules are
 314 *                    clock gated or not, and update the soft clock states
 315 *                    structure. It is required to be called when the partition
 316 *                    is requested so that the driver knows which modules are
 317 *                    clock gated when the partition is requested. This function
 318 *                    expects the caller to apply partition lock before calling
 319 *                    this function.
 320 * @set_part_clocks: set partition modules clocks gate registers based on the
 321 *                   partition clock states bitmap. This function expects the
 322 *                   caller to apply partition lock before calling this
 323 *                   function. The caller function will need to set the bitmap
 324 *                   on which tiles are required to be clocked on.
 325 *
 326 * Different AI engine device version has its own device
 327 * operation.
 328 */
 329struct aie_tile_operations {
 330        u32 (*get_tile_type)(struct aie_location *loc);
 331        unsigned int (*get_mem_info)(struct aie_range *range,
 332                                     struct aie_part_mem *pmem);
 333        u32 (*get_core_status)(struct aie_partition *apart,
 334                               struct aie_location *loc);
 335        int (*reset_shim)(struct aie_device *adev, struct aie_range *range);
 336        int (*init_part_clk_state)(struct aie_partition *apart);
 337        int (*scan_part_clocks)(struct aie_partition *apart);
 338        int (*set_part_clocks)(struct aie_partition *apart);
 339};
 340
 341/**
 342 * struct aie_resource - AI engine resource structure
 343 * @bitmap: resource bitmap
 344 * @total: total number of resource
 345 */
 346struct aie_resource {
 347        unsigned long *bitmap;
 348        u32 total;
 349};
 350
 351/**
 352 * struct aie_event_attr - AI Engine event attributes structure.
 353 * @bc_event: broadcast event attribute to capture event mask value and
 354 *            register offset from @bc_regoff.
 355 * @group_error: group error attribute to capture error group mask value and
 356 *               register offset value from @group_regoff.
 357 * @bc_regoff: base broadcast register offset.
 358 * @status_regoff: base status register offset.
 359 * @group_regoff: base group error register offset.
 360 * @base_error_event: event ID of first error event in a group error.
 361 * @num_broadcasts: total number of broadcast events.
 362 * @base_bc_event: broadcast 0 vent ID
 363 * @num_events: total number of events.
 364 */
 365struct aie_event_attr {
 366        struct aie_single_reg_field bc_event;
 367        struct aie_single_reg_field group_error;
 368        u32 bc_regoff;
 369        u32 status_regoff;
 370        u32 group_regoff;
 371        u32 base_error_event;
 372        u32 num_broadcasts;
 373        u32 base_bc_event;
 374        u32 num_events;
 375};
 376
 377/**
 378 * struct aie_l1_intr_ctrl_attr - AI engine level 1 interrupt controller
 379 *                                attributes structure.
 380 * @mask: level 1 interrupt controller mask attribute.
 381 * @swa_status: switch A level 1 interrupt controller status attribute.
 382 * @swb_status: switch A level 1 interrupt controller status attribute.
 383 * @swa_event: switch A level 1 interrupt controller event attribute.
 384 * @swb_event: switch A level 1 interrupt controller event attribute.
 385 * @regoff: base level 1 interrupt controller register offset.
 386 * @event_lsb: lsb of IRQ event within IRQ event switch register.
 387 * @num_broadcasts: total number of broadcast signals to level 1 interrupt
 388 *                  controller.
 389 */
 390struct aie_l1_intr_ctrl_attr {
 391        struct aie_single_reg_field swa_status;
 392        struct aie_single_reg_field swb_status;
 393        struct aie_single_reg_field swa_event;
 394        struct aie_single_reg_field swb_event;
 395        u32 regoff;
 396        u32 event_lsb;
 397        u32 num_broadcasts;
 398};
 399
 400/**
 401 * struct aie_l2_intr_ctrl_attr - AI engine level 2 interrupt controller
 402 *                                attributes structure.
 403 * @mask: level 2 interrupt controller mask attribute.
 404 * @enable: level 2 interrupt controller enable attribute.
 405 * @disable: level 2 interrupt controller disable attribute.
 406 * @status: level 2 interrupt controller status attribute.
 407 * @regoff: level 2 interrupt controller register offset.
 408 * @num_broadcasts: total number of broadcast signals to level 2 interrupt
 409 *                  controller.
 410 */
 411struct aie_l2_intr_ctrl_attr {
 412        struct aie_single_reg_field mask;
 413        struct aie_single_reg_field enable;
 414        struct aie_single_reg_field disable;
 415        struct aie_single_reg_field status;
 416        u32 regoff;
 417        u32 num_broadcasts;
 418};
 419
 420/**
 421 * struct aie_error_cb - AI engine error callback struct.
 422 * @cb: pointer to callback function.
 423 * @priv: data to be passed to the callback function.
 424 */
 425struct aie_error_cb {
 426        void (*cb)(void *priv);
 427        void *priv;
 428};
 429
 430/**
 431 * struct aie_event_prop - AI engine event property.
 432 * @event: error event ID.
 433 * @event_str: error string.
 434 */
 435struct aie_event_prop {
 436        u32 event;
 437        char *event_str;
 438};
 439
 440/**
 441 * struct aie_err_category - AI engine errors category.
 442 * @err_category: category of error.
 443 * @num_events: number of event IDs in a category.
 444 * @prop: pointer to an array event properties.
 445 */
 446struct aie_err_category {
 447        u32 err_category;
 448        u32 num_events;
 449        const struct aie_event_prop *prop;
 450};
 451
 452/**
 453 * struct aie_error_attr - AI engine error attribute.
 454 * @num_err_categories: number of possible error categories valid for a given
 455 *                      module.
 456 * @err_category: pointer to an array of error categories.
 457 */
 458struct aie_error_attr {
 459        u32 num_err_categories;
 460        const struct aie_err_category *err_category;
 461};
 462
 463/**
 464 * struct aie_rsc_stat - AI engine hardware resource status bitmap of a
 465 *                       resource of a module type of a tile type of an AI
 466 *                       engine partition
 467 * @rbits: runtime allocated resource bitmap
 468 * @sbits: static resource bitmap for resources allocated at compilation
 469 *         time
 470 */
 471struct aie_rsc_stat {
 472        struct aie_resource rbits;
 473        struct aie_resource sbits;
 474};
 475
 476/**
 477 * struct aie_mod_rscs - AI engine hardware resource status bitmaps of
 478 *                       a module type of a tile type of an AI engine
 479 *                       partition.
 480 * @rscs_stat: resource status bitmaps
 481 */
 482struct aie_mod_rscs {
 483        struct aie_rsc_stat *rscs_stat;
 484};
 485
 486/**
 487 * struct aie_tile_rscs - AI engine hardware resource status bitmaps of all
 488 *                        resources of a tile type of a partition.
 489 * @mod_rscs: array of pointers of AI engine resources. Each element is an
 490 *            array of hardware resources of different modules of a particular
 491 *            resource type of a tile type.
 492 *            e.g. if the tile type is TILE. The rscs are an arrary of
 493 *            resources bitmap of all the defined AI engine resources types of
 494 *            TILE type. e.g. the element of AIE_RSCTYPE_PERF. It is an array
 495 *            of perfcounter resources bitmaps for both core module and memory
 496 *            module of TILE type of an AI engine partition.
 497 */
 498struct aie_tile_rscs {
 499        struct aie_mod_rscs *mod_rscs[AIE_RSCTYPE_MAX];
 500};
 501
 502/**
 503 * struct aie_mod_rsc_attr - AI engine resource attribute of a module
 504 * @num_rscs: number of resource
 505 */
 506struct aie_mod_rsc_attr {
 507        u8 num_rscs;
 508};
 509
 510/**
 511 * struct aie_tile_rsc_attr - AI engine resource attributes
 512 * @mod_attr: array of resource attribute different modules of a tile type of
 513 *            a particular resource type.
 514 */
 515struct aie_tile_rsc_attr {
 516        struct aie_mod_rsc_attr mod_attr[AIE_MAX_MODS_PER_TILE];
 517};
 518
 519/**
 520 * struct aie_lock_attr - AI engine lock attributes
 521 * @sts: lock status field attributes
 522 * @sts_regoff: lock status register offset
 523 * @num_locks: number of locks
 524 */
 525struct aie_lock_attr {
 526        struct aie_single_reg_field sts;
 527        u32 sts_regoff;
 528        u32 num_locks;
 529};
 530
 531/**
 532 * struct aie_tile_attr - AI engine device tile type attributes
 533 * @start_row: start row
 534 * @num_rows: number of rows
 535 * @num_mods: number of modules of this tile type
 536 * @mods: array of module types of this tile type
 537 * @rscs_attr: resources attributes array. Each element is an array of
 538 *             attributes of a resource type of a tile type.
 539 */
 540struct aie_tile_attr {
 541        u8 start_row;
 542        u8 num_rows;
 543        u8 num_mods;
 544        const enum aie_module_type *mods;
 545        const struct aie_tile_rsc_attr *rscs_attr;
 546};
 547
 548/**
 549 * struct aie_dev_attr - device attribute properties for AI Engine sysfs nodes.
 550 * @name: name of the device attribute
 551 * @mode: permissions associated
 552 * @tile_type: tile type(s) attribute is valid for. use AIE_TILE_TYPE_MASK_*.
 553 * @show: read function handler
 554 * @store: write function handler
 555 */
 556struct aie_dev_attr {
 557        const char *name;
 558        umode_t mode;
 559        u32 tile_type;
 560        ssize_t (*show)(struct device *dev, struct device_attribute *attr,
 561                        char *buf);
 562        ssize_t (*store)(struct device *dev, struct device_attribute *attr,
 563                         const char *buf, size_t count);
 564};
 565
 566/**
 567 * struct aie_sysfs_prop - private data passed to the sysfs read/write handler.
 568 * @data: buffer to export sysfs data
 569 * @size: size of data exported
 570 * @max_size: max size of data that could be exported
 571 * @read_callback: callback to fetch data from on read
 572 * @write_callback: callback to send data to on write
 573 */
 574struct aie_sysfs_prop {
 575        char *data;
 576        ssize_t size;
 577        ssize_t max_size;
 578        ssize_t (*read_callback)(struct kobject *kobj, char *buffer,
 579                                 ssize_t size);
 580        ssize_t (*write_callback)(struct kobject *kobj, char *buffer,
 581                                  ssize_t size);
 582};
 583
 584/**
 585 * struct aie_bin_attr - binary attribute properties for AI Engine sysfs nodes
 586 * @name: name of the binary attribute
 587 * @mode: permissions associated
 588 * @size: size of the buffer to be allocated
 589 * @tile_type: tile type(s) attribute is valid for. use AIE_TILE_TYPE_MASK_*.
 590 * @read: read handler
 591 * @write: write handler
 592 * @read_callback: callback to fetch data from on read
 593 * @write_callback:  callback to send data to on write
 594 */
 595struct aie_bin_attr {
 596        const char *name;
 597        umode_t mode;
 598        ssize_t size;
 599        u32 tile_type;
 600        ssize_t (*read)(struct file *filp, struct kobject *kobj,
 601                        struct bin_attribute *attr, char *buf, loff_t offset,
 602                        size_t max_size);
 603        ssize_t (*write)(struct file *filp, struct kobject *kobj,
 604                         struct bin_attribute *attr, char *buf, loff_t offset,
 605                         size_t max_size);
 606        ssize_t (*read_callback)(struct kobject *kobj, char *buffer,
 607                                 ssize_t size);
 608        ssize_t (*write_callback)(struct kobject *kobj, char *buffer,
 609                                  ssize_t size);
 610};
 611
 612/**
 613 * struct aie_sysfs_attr - captures all sysfs attributes defined at
 614 *                         partition or tile level.
 615 * @dev_attr: pointer to array of device attributes
 616 * @bin_attr: pointer to array of binary attributes
 617 * @num_dev_attrs: number of device attributes
 618 * @num_bin_attrs: number of binary attributes
 619 */
 620struct aie_sysfs_attr {
 621        const struct aie_dev_attr *dev_attr;
 622        const struct aie_bin_attr *bin_attr;
 623        u32 num_dev_attrs;
 624        u32 num_bin_attrs;
 625};
 626
 627/**
 628 * struct aie_tile - AI engine tile structure
 629 * @loc: tile co-ordinates
 630 * @apart: parent partition the tile belongs to
 631 * @dev: device for the AI engine tile device
 632 * @attr_grp: attribute group
 633 */
 634struct aie_tile {
 635        struct aie_location loc;
 636        struct aie_partition *apart;
 637        struct device dev;
 638        struct attribute_group *attr_grp;
 639};
 640
 641/**
 642 * struct aie_device - AI engine device structure
 643 * @partitions: list of partitions requested
 644 * @cdev: cdev for the AI engine
 645 * @dev: device for the AI engine device
 646 * @mlock: protection for AI engine device operations
 647 * @base: AI engine device base virtual address
 648 * @clk: AI enigne device clock
 649 * @res: memory resource of AI engine device
 650 * @kernel_regs: array of kernel only registers
 651 * @core_regs: array of core registers
 652 * @ops: tile operations
 653 * @col_rst: column reset attribute
 654 * @col_clkbuf: column clock buffer attribute
 655 * @shim_dma: SHIM DMA attribute
 656 * @tile_dma: tile DMA attribute
 657 * @pl_events: pl module event attribute
 658 * @mem_events: memory module event attribute
 659 * @core_events: core module event attribute
 660 * @l1_ctrl: level 1 interrupt controller attribute
 661 * @l2_ctrl: level 2 interrupt controller attribute
 662 * @core_errors: core module error attribute
 663 * @mem_errors: memory module error attribute
 664 * @shim_errors: shim tile error attribute
 665 * @size: size of the AI engine address space
 666 * @array_shift: array address shift
 667 * @col_shift: column address shift
 668 * @row_shift: row address shift
 669 * @cols_res: AI engine columns resources to indicate
 670 *            while columns are occupied by partitions.
 671 * @num_kernel_regs: number of kernel only registers range
 672 * @num_core_regs: number of core registers range
 673 * @irq: Linux IRQ number
 674 * @backtrack: workqueue to backtrack interrupt
 675 * @version: AI engine device version
 676 * @pm_node_id: AI Engine platform management node ID
 677 * @clock_id: AI Engine clock ID
 678 * @ttype_attr: tile type attributes
 679 * @part_sysfs_attr: partition level sysfs attributes
 680 * @tile_sysfs_attr: tile level sysfs attributes
 681 * @core_status_str: core status in string format
 682 * @core_pc: program counter attribute
 683 * @core_lr: link register attribute
 684 * @core_sp: stack pointer attribute
 685 * @dma_status_str: DMA channel status in string format
 686 * @queue_status_str: DMA queue status in string format
 687 * @pl_lock: PL module lock attribute
 688 * @mem_lock: memory module lock attribute
 689 * @lock_status_str: lock status in string format
 690 */
 691struct aie_device {
 692        struct list_head partitions;
 693        struct cdev cdev;
 694        struct device dev;
 695        struct mutex mlock; /* protection for AI engine partitions */
 696        void __iomem *base;
 697        struct clk *clk;
 698        struct resource *res;
 699        const struct aie_tile_regs *kernel_regs;
 700        const struct aie_core_regs_attr *core_regs;
 701        const struct aie_tile_operations *ops;
 702        const struct aie_single_reg_field *col_rst;
 703        const struct aie_single_reg_field *col_clkbuf;
 704        const struct aie_dma_attr *shim_dma;
 705        const struct aie_dma_attr *tile_dma;
 706        const struct aie_event_attr *pl_events;
 707        const struct aie_event_attr *mem_events;
 708        const struct aie_event_attr *core_events;
 709        const struct aie_l1_intr_ctrl_attr *l1_ctrl;
 710        const struct aie_l2_intr_ctrl_attr *l2_ctrl;
 711        const struct aie_error_attr *core_errors;
 712        const struct aie_error_attr *mem_errors;
 713        const struct aie_error_attr *shim_errors;
 714        size_t size;
 715        struct aie_resource cols_res;
 716        u32 array_shift;
 717        u32 col_shift;
 718        u32 row_shift;
 719        u32 num_kernel_regs;
 720        u32 num_core_regs;
 721        int irq;
 722        struct work_struct backtrack;
 723        int version;
 724        u32 pm_node_id;
 725        u32 clock_id;
 726        struct aie_tile_attr ttype_attr[AIE_TILE_TYPE_MAX];
 727        const struct aie_sysfs_attr *part_sysfs_attr;
 728        const struct aie_sysfs_attr *tile_sysfs_attr;
 729        char **core_status_str;
 730        const struct aie_single_reg_field *core_pc;
 731        const struct aie_single_reg_field *core_lr;
 732        const struct aie_single_reg_field *core_sp;
 733        char **dma_status_str;
 734        char **queue_status_str;
 735        const struct aie_lock_attr *pl_lock;
 736        const struct aie_lock_attr *mem_lock;
 737        char **lock_status_str;
 738};
 739
 740/**
 741 * struct aie_part_bridge - AI engine FPGA bridge
 742 * @name: name of the FPGA bridge
 743 * @br: pointer to FPGA bridge
 744 */
 745struct aie_part_bridge {
 746        char name[32];
 747        struct fpga_bridge *br;
 748};
 749
 750/**
 751 * struct aie_partition - AI engine partition structure
 752 * @node: list node
 753 * @dbufs: dmabufs list
 754 * @adev: pointer to AI device instance
 755 * @filep: pointer to file for refcount on the users of the partition
 756 * @pmems: pointer to partition memories types
 757 * @dbufs_cache: memory management object for preallocated dmabuf descriptors
 758 * @trscs: resources bitmaps for each tile
 759 * @freq_req: required frequency
 760 * @br: AI engine FPGA bridge
 761 * @range: range of partition
 762 * @mlock: protection for AI engine partition operations
 763 * @dev: device for the AI engine partition
 764 * @atiles: pointer to an array of AIE tile structure.
 765 * @cores_clk_state: bitmap to indicate the power state of core modules
 766 * @tiles_inuse: bitmap to indicate if a tile is in use
 767 * @error_cb: error callback
 768 * @core_event_status: core module event bitmap
 769 * @mem_event_status: memory module event bitmap
 770 * @pl_event_status: pl module event bitmap
 771 * @l2_mask: level 2 interrupt controller mask bitmap
 772 * @attr_grp: attribute group
 773 * @partition_id: partition id. Partition ID is the identifier
 774 *                of the AI engine partition in the system.
 775 * @status: indicate if the partition is in use
 776 * @cntrflag: partition control flag. e.g. whether to reset columns when
 777 *            the partition is released
 778 * @error_to_report: indicates if there are errors pending to be reported to
 779 *                   the application. This value is set to true if errors are
 780 *                   found during backtracking, and error interrupt was
 781 *                   received when partition was not requested yet.
 782 */
 783struct aie_partition {
 784        struct list_head node;
 785        struct list_head dbufs;
 786        struct aie_part_bridge br;
 787        struct aie_device *adev;
 788        struct file *filep;
 789        struct aie_part_mem *pmems;
 790        struct kmem_cache *dbufs_cache;
 791        struct aie_tile_rscs trscs[AIE_TILE_TYPE_MAX];
 792        u64 freq_req;
 793        struct aie_range range;
 794        struct mutex mlock; /* protection for AI engine partition operations */
 795        struct device dev;
 796        struct aie_tile *atiles;
 797        struct aie_resource cores_clk_state;
 798        struct aie_resource tiles_inuse;
 799        struct aie_error_cb error_cb;
 800        struct aie_resource core_event_status;
 801        struct aie_resource mem_event_status;
 802        struct aie_resource pl_event_status;
 803        struct aie_resource l2_mask;
 804        struct attribute_group *attr_grp;
 805        u32 partition_id;
 806        u32 status;
 807        u32 cntrflag;
 808        u8 error_to_report;
 809};
 810
 811/**
 812 * struct aie_part_pinned_region - AI engine user space pinned region
 813 * @user_addr: user space address
 814 * @len: length of the user space buffer in bytes
 815 * @npages: number of pages of the user space buffer
 816 * @pages: array to receive pointers to the pages pinned.
 817 *         should be at least npages long
 818 */
 819struct aie_part_pinned_region {
 820        u64 user_addr;
 821        u64 len;
 822        struct page **pages;
 823        int npages;
 824};
 825
 826extern struct class *aie_class;
 827extern const struct file_operations aie_part_fops;
 828
 829#define cdev_to_aiedev(i_cdev) container_of((i_cdev), struct aie_device, cdev)
 830#define dev_to_aiedev(_dev) container_of((_dev), struct aie_device, dev)
 831#define dev_to_aiepart(_dev) container_of((_dev), struct aie_partition, dev)
 832#define dev_to_aietile(_dev) container_of((_dev), struct aie_tile, dev)
 833
 834#define aie_col_mask(adev) ({ \
 835        struct aie_device *_adev = (adev); \
 836        GENMASK_ULL(_adev->array_shift - 1, _adev->col_shift);  \
 837        })
 838
 839#define aie_row_mask(adev) ({ \
 840        struct aie_device *_adev = (adev); \
 841        GENMASK_ULL(_adev->col_shift - 1, _adev->row_shift);  \
 842        })
 843
 844#define aie_tile_reg_mask(adev) ({ \
 845        struct aie_device *_adev = (adev); \
 846        GENMASK_ULL(_adev->row_shift - 1, 0);  \
 847        })
 848
 849/*
 850 * Need to define field get, as AI engine shift mask is not constant.
 851 * Cannot use FIELD_GET()
 852 */
 853#define aie_tile_reg_field_get(mask, shift, regoff) ( \
 854        ((regoff) & (mask)) >> (shift))
 855
 856#define aie_cal_tile_reg(adev, regoff) ( \
 857        aie_tile_reg_field_get(aie_tile_reg_mask(adev), 0, regoff))
 858
 859/**
 860 * aie_get_field_val() - calculate value of an AI engine register field
 861 * @field: a field in a register
 862 * @val: value of the field
 863 * @return: value of a register field
 864 */
 865static inline u32 aie_get_field_val(const struct aie_single_reg_field *field,
 866                                    u32 val)
 867{
 868        long long mask = (long long)field->mask & 0x00000000ffffffff;
 869
 870        return (val << __bf_shf(mask)) & field->mask;
 871}
 872
 873/**
 874 * aie_get_reg_field() - get value from a field from a register valuer
 875 * @field: a field in a register
 876 * @regval: register value
 877 * @return: value of a register field
 878 */
 879static inline u32 aie_get_reg_field(const struct aie_single_reg_field *field,
 880                                    u32 regval)
 881{
 882        long long mask64 = (long long)field->mask & 0x00000000ffffffff;
 883
 884        return (regval & field->mask) >> __bf_shf(mask64);
 885}
 886
 887/**
 888 * aie_cal_regoff() - calculate register offset to the whole AI engine
 889 *                    device start address
 890 * @adev: AI engine device
 891 * @loc: AI engine tile location
 892 * @regoff_intile: register offset within a tile
 893 * @return: register offset to the whole AI engine device start address
 894 */
 895static inline u32 aie_cal_regoff(struct aie_device *adev,
 896                                 struct aie_location loc, u32 regoff_intile)
 897{
 898        return regoff_intile + (loc.col << adev->col_shift) +
 899               (loc.row << adev->row_shift);
 900}
 901
 902/**
 903 * aie_validate_location() - validate tile location within an AI engine
 904 *                           partition
 905 * @apart: AI engine partition
 906 * @loc: AI engine tile location
 907 * @return: return 0 if it is valid, negative value for errors.
 908 *
 909 * This function checks if the AI engine location is within the AI engine
 910 * partition.
 911 */
 912static inline int aie_validate_location(struct aie_partition *apart,
 913                                        struct aie_location loc)
 914{
 915        if (loc.col < apart->range.start.col ||
 916            loc.col >= apart->range.start.col + apart->range.size.col ||
 917            loc.row < apart->range.start.row ||
 918            loc.row >= apart->range.start.row + apart->range.size.row)
 919                return -EINVAL;
 920
 921        return 0;
 922}
 923
 924/**
 925 * aie_resource_or_get_valueul() - get unsigned long value of specified
 926 *                                 number of bits starting from specified
 927 *                                 start bit of a resource bitmap
 928 *
 929 * @res: pointer to AI engine resource
 930 * @sbit: start bit for OR operation
 931 * @nbits: number of bits to OR
 932 * @return: or result of @nbits of two bitmaps starting from @sbit
 933 *
 934 * OR @nbits of two resource bitmaps starting from @sbit
 935 */
 936static inline
 937unsigned long aie_resource_or_get_valueul(struct aie_resource *res,
 938                                          u32 sbit, u32 nbits)
 939{
 940        const size_t i = BIT_WORD(sbit);
 941        unsigned long bits;
 942
 943        bits = res->bitmap[i];
 944        bits >>= (sbit % BITS_PER_LONG);
 945        bits |= BITMAP_FIRST_WORD_MASK(nbits);
 946
 947        return bits;
 948}
 949
 950int aie_resource_initialize(struct aie_resource *res, int count);
 951void aie_resource_uninitialize(struct aie_resource *res);
 952int aie_resource_check_region(struct aie_resource *res, u32 start,
 953                              u32 count);
 954int aie_resource_get_region(struct aie_resource *res, u32 start,
 955                            u32 count);
 956void aie_resource_put_region(struct aie_resource *res, int start, u32 count);
 957int aie_resource_set(struct aie_resource *res, u32 start, u32 count);
 958int aie_resource_cpy_from_arr32(struct aie_resource *res, u32 start,
 959                                const u32 *src, u32 nbits);
 960int aie_resource_cpy_to_arr32(struct aie_resource *res, u32 start, u32 *dst,
 961                              u32 nbits);
 962int aie_resource_clear(struct aie_resource *res, u32 start, u32 count);
 963int aie_resource_clear_all(struct aie_resource *res);
 964bool aie_resource_testbit(struct aie_resource *res, u32 bit);
 965int aie_resource_check_common_avail(struct aie_resource *res0,
 966                                    struct aie_resource *res1,
 967                                    u32 sbit, u32 nbits);
 968int aie_resource_get_common_avail(struct aie_resource *res0,
 969                                  struct aie_resource *res1,
 970                                  u32 sbit, u32 nbits, u32 total,
 971                                  struct aie_rsc *rscs);
 972int aie_resource_check_pattern_region(struct aie_resource *res,
 973                                      u32 start, u32 end, u32 count);
 974int aie_resource_check_common_pattern_region(struct aie_resource *res0,
 975                                             struct aie_resource *res1,
 976                                             u32 sbit, u32 nbits, u32 total);
 977int aie_resource_get_common_pattern_region(struct aie_resource *res0,
 978                                           struct aie_resource *res1,
 979                                           u32 sbit, u32 nbits, u32 total,
 980                                           struct aie_rsc *rscs);
 981
 982const struct file_operations *aie_part_get_fops(void);
 983u8 aie_part_in_use(struct aie_partition *apart);
 984struct aie_partition *aie_get_partition_from_id(struct aie_device *adev,
 985                                                u32 partition_id);
 986struct aie_partition *of_aie_part_probe(struct aie_device *adev,
 987                                        struct device_node *nc);
 988void aie_part_remove(struct aie_partition *apart);
 989int aie_part_clean(struct aie_partition *apart);
 990int aie_part_open(struct aie_partition *apart, void *rsc_metadata);
 991
 992int aie_fpga_create_bridge(struct aie_partition *apart);
 993void aie_fpga_free_bridge(struct aie_partition *apart);
 994
 995int aie_mem_get_info(struct aie_partition *apart, unsigned long arg);
 996
 997long aie_part_attach_dmabuf_req(struct aie_partition *apart,
 998                                void __user *user_args);
 999long aie_part_detach_dmabuf_req(struct aie_partition *apart,
1000                                void __user *user_args);
1001long aie_part_set_bd(struct aie_partition *apart, void __user *user_args);
1002long aie_part_set_dmabuf_bd(struct aie_partition *apart,
1003                            void __user *user_args);
1004void aie_part_release_dmabufs(struct aie_partition *apart);
1005int aie_part_prealloc_dbufs_cache(struct aie_partition *apart);
1006
1007int aie_part_scan_clk_state(struct aie_partition *apart);
1008bool aie_part_check_clk_enable_loc(struct aie_partition *apart,
1009                                   struct aie_location *loc);
1010int aie_part_set_freq(struct aie_partition *apart, u64 freq);
1011int aie_part_get_running_freq(struct aie_partition *apart, u64 *freq);
1012
1013int aie_part_request_tiles_from_user(struct aie_partition *apart,
1014                                     void __user *user_args);
1015int aie_part_release_tiles_from_user(struct aie_partition *apart,
1016                                     void __user *user_args);
1017int aie_device_init(struct aie_device *adev);
1018
1019void aie_array_backtrack(struct work_struct *work);
1020irqreturn_t aie_interrupt(int irq, void *data);
1021void aie_part_clear_cached_events(struct aie_partition *apart);
1022int aie_part_set_intr_rscs(struct aie_partition *apart);
1023
1024bool aie_part_has_mem_mmapped(struct aie_partition *apart);
1025bool aie_part_has_regs_mmapped(struct aie_partition *apart);
1026
1027int aie_part_get_tile_rows(struct aie_partition *apart,
1028                           enum aie_tile_type ttype);
1029
1030int aie_part_reset(struct aie_partition *apart);
1031int aie_part_post_reinit(struct aie_partition *apart);
1032
1033int aie_part_rscmgr_init(struct aie_partition *apart);
1034void aie_part_rscmgr_finish(struct aie_partition *apart);
1035void aie_part_rscmgr_reset(struct aie_partition *apart);
1036long aie_part_rscmgr_rsc_req(struct aie_partition *apart,
1037                             void __user *user_args);
1038long aie_part_rscmgr_rsc_release(struct aie_partition *apart,
1039                                 void __user *user_args);
1040long aie_part_rscmgr_rsc_free(struct aie_partition *apart,
1041                              void __user *user_args);
1042long aie_part_rscmgr_rsc_req_specific(struct aie_partition *apart,
1043                                      void __user *user_args);
1044long aie_part_rscmgr_rsc_check_avail(struct aie_partition *apart,
1045                                     void __user *user_args);
1046long aie_part_rscmgr_get_broadcast(struct aie_partition *apart,
1047                                   void __user *user_args);
1048int aie_part_rscmgr_set_static(struct aie_partition *apart, void *meta);
1049int aie_part_rscmgr_set_tile_broadcast(struct aie_partition *apart,
1050                                       struct aie_location loc,
1051                                       enum aie_module_type mod, uint32_t id);
1052
1053int aie_part_sysfs_create_entries(struct aie_partition *apart);
1054void aie_part_sysfs_remove_entries(struct aie_partition *apart);
1055int aie_tile_sysfs_create_entries(struct aie_tile *atile);
1056void aie_tile_sysfs_remove_entries(struct aie_tile *atile);
1057ssize_t aie_sysfs_read_handler(struct file *filp, struct kobject *kobj,
1058                               struct bin_attribute *attr, char *buf,
1059                               loff_t offset, size_t max_size);
1060
1061ssize_t aie_sysfs_get_core_status(struct aie_partition *apart,
1062                                  struct aie_location *loc, char *buffer,
1063                                  ssize_t size);
1064ssize_t aie_tile_show_core(struct device *dev, struct device_attribute *attr,
1065                           char *buffer);
1066ssize_t aie_part_read_cb_core(struct kobject *kobj, char *buffer, ssize_t size);
1067ssize_t aie_sysfs_get_dma_status(struct aie_partition *apart,
1068                                 struct aie_location *loc, char *buffer,
1069                                 ssize_t size);
1070ssize_t aie_tile_show_dma(struct device *dev, struct device_attribute *attr,
1071                          char *buffer);
1072ssize_t aie_part_read_cb_dma(struct kobject *kobj, char *buffer, ssize_t size);
1073ssize_t aie_tile_show_lock(struct device *dev, struct device_attribute *attr,
1074                           char *buffer);
1075ssize_t aie_part_read_cb_lock(struct kobject *kobj, char *buffer, ssize_t size);
1076ssize_t aie_sysfs_get_lock_status(struct aie_partition *apart,
1077                                  struct aie_location *loc, char *buffer,
1078                                  ssize_t size);
1079u32 aie_get_module_error_count(struct aie_partition *apart,
1080                               struct aie_location loc,
1081                               enum aie_module_type module,
1082                               const struct aie_error_attr *err_attr);
1083bool aie_check_error_bitmap(struct aie_partition *apart,
1084                            struct aie_location loc,
1085                            enum aie_module_type module, u8 event);
1086u32 aie_get_error_count(struct aie_partition *apart);
1087ssize_t aie_sysfs_get_errors(struct aie_partition *apart,
1088                             struct aie_location *loc, char *buffer,
1089                             ssize_t size);
1090ssize_t aie_tile_show_error(struct device *dev, struct device_attribute *attr,
1091                            char *buffer);
1092ssize_t aie_part_show_error_stat(struct device *dev,
1093                                 struct device_attribute *attr, char *buffer);
1094ssize_t aie_part_read_cb_error(struct kobject *kobj, char *buffer,
1095                               ssize_t size);
1096ssize_t aie_tile_show_event(struct device *dev, struct device_attribute *attr,
1097                            char *buffer);
1098void aie_read_event_status(struct aie_partition *apart,
1099                           struct aie_location *loc,
1100                           enum aie_module_type module, u32 *reg);
1101ssize_t aie_part_read_cb_status(struct kobject *kobj, char *buffer,
1102                                ssize_t size);
1103long aie_part_rscmgr_get_statistics(struct aie_partition *apart,
1104                                    void __user *user_args);
1105
1106#endif /* AIE_INTERNAL_H */
1107