Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple pulp-sdk and add hwpe device structure #1

Merged
merged 4 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# Changelog

## [Unreleased]

### Added

- New Hardware Processing Engine (HWPE) device in `util/hwpe.h`
- A device structure for ne16 `ne16_dev_t` in `ne16/hal/ne16.h` which extends the hwpe device
- Test app Makefile has now an `ACCELERATOR` variable to specify which accelerator is used

### Changed

- Library functions no longer start with a generic `nnx_` prefix but with `<accelerator>_nnx_` prefix
to allow for usage of multiple kinds of accelerators in the same system
- Decoupled board specific functionality into `ne16/bsp` which also contains constant global structures
to the implementations of the `ne16_dev_t` structure
- Moved all task related functions (`nnx_task_set_dims*`) into `ne16/hal/ne16_task.c`
- Tests adjusted for the new interface
- Test data generation moved into source files with extern declarations to check the output from the main

### Fixed

- pyright errors
- formatting errors

## [0.2.1] - 2024-01-08

### Fixed
Expand Down
75 changes: 0 additions & 75 deletions inc/pulp_nnx.h

This file was deleted.

77 changes: 77 additions & 0 deletions inc/pulp_nnx_ne16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Luka Macan <[email protected]>
*
* Copyright 2023 ETH Zurich and University of Bologna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "ne16.h"
#include "ne16_pulp_bsp.h"
#include "ne16_task.h"
#include <stdint.h>

/* PULP-NNX interface */

void ne16_nnx_init(ne16_dev_t *dev, ne16_pulp_conf_t *conf);
void ne16_nnx_term(ne16_dev_t *dev);

/** ne16_nnx_dispatch_check
*
* Check whether you can dispatch to the accelerator.
*/
int ne16_nnx_dispatch_check(ne16_dev_t *dev);

/** ne16_nnx_dispatch_wait
*
* Block until you can dispatch to the accelerator.
*/
void ne16_nnx_dispatch_wait(ne16_dev_t *dev);

/** ne16_nnx_dispatch
*
* Dispatch a task to the accelerator.
* Fails with return code 1 if the task cannot be dispatched. Otherwise returns 0.
*/
int ne16_nnx_dispatch(ne16_dev_t *dev, ne16_task_t *task);

/** ne16_nnx_resolve_check
*
* Check whether the task has been resolved.
*/
int ne16_nnx_resolve_check(ne16_dev_t *dev, ne16_task_t *task);

/** ne16_nnx_resolve_wait
*
* Block until you can resolve the task.
*/
void ne16_nnx_resolve_wait(ne16_dev_t *dev, ne16_task_t *task);


/* Additional helper functions */

/** ne16_nnx_dispatch_stride2x2
*
* It uses NE16's 2x2 strided mode which reduces the number of writes NE16 does.
* This mode doesn't stride the NE16's subtile input pointer, so we have to
* tile the tile to the subtile's spatial dimensions (in this case 3x3 output).
* Works only if the k_out is divisible by 2.
*/
void ne16_nnx_dispatch_stride2x2(
ne16_dev_t *dev, ne16_task_t *task, const uint32_t w_in, const uint32_t k_in,
const uint32_t w_in_stride, const uint32_t k_in_stride,
const uint32_t h_out, const uint32_t w_out, const uint32_t k_out,
const uint32_t w_out_stride, const uint32_t k_out_stride,
const uint8_t h_ker, const uint8_t w_ker);
85 changes: 85 additions & 0 deletions ne16/bsp/ne16_pulp_bsp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Luka Macan <[email protected]>
*
* Copyright 2023 ETH Zurich and University of Bologna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "ne16_pulp_bsp.h"
#include <pmsis.h>

#define NE16_PULP_CLUSTER_CTRL_ADDR_BASE (0x00200000)
#define NE16_PULP_CLUSTER_CTRL_HWPE_OFFS 0x18
#define NE16_PULP_CLUSTER_CTRL_HWPE_ADDR \
(NE16_PULP_CLUSTER_CTRL_ADDR_BASE + NE16_PULP_CLUSTER_CTRL_HWPE_OFFS)
#define NE16_PULP_CLUSTER_CTRL_HWPE_MASK_CG_EN 0x800
#define NE16_PULP_CLUSTER_CTRL_HWPE_MASK_HCI_PRIO 0x100
#define NE16_PULP_CLUSTER_CTRL_HWPE_MASK_HCI_MAXSTALL 0xff
#define NE16_PULP_MAX_STALL (8)
#define NE16_PULP_EVENT (1 << 12)
#define NE16_PULP_BASE_ADDR (0x00201000)

void ne16_pulp_cg_enable() {
*(volatile uint32_t *)NE16_PULP_CLUSTER_CTRL_HWPE_ADDR |=
NE16_PULP_CLUSTER_CTRL_HWPE_MASK_CG_EN;
}

void ne16_pulp_cg_disable() {
*(volatile uint32_t *)NE16_PULP_CLUSTER_CTRL_HWPE_ADDR &=
~NE16_PULP_CLUSTER_CTRL_HWPE_MASK_CG_EN;
}

void ne16_pulp_hci_setpriority_ne16() {
*(volatile uint32_t *)NE16_PULP_CLUSTER_CTRL_HWPE_ADDR |=
NE16_PULP_CLUSTER_CTRL_HWPE_MASK_HCI_PRIO;
}

void ne16_pulp_hci_setpriority_core() {
*(volatile uint32_t *)NE16_PULP_CLUSTER_CTRL_HWPE_ADDR &=
~NE16_PULP_CLUSTER_CTRL_HWPE_MASK_HCI_PRIO;
}

void ne16_pulp_hci_reset_max_stall() {
*(volatile uint32_t *)NE16_PULP_CLUSTER_CTRL_HWPE_ADDR &=
~NE16_PULP_CLUSTER_CTRL_HWPE_MASK_HCI_MAXSTALL;
}

void ne16_pulp_hci_set_max_stall(uint32_t max_stall) {
*(volatile uint32_t *)NE16_PULP_CLUSTER_CTRL_HWPE_ADDR |=
max_stall & NE16_PULP_CLUSTER_CTRL_HWPE_MASK_HCI_MAXSTALL;
}

void ne16_pulp_open(ne16_pulp_conf_t *conf) {
ne16_pulp_cg_enable();
ne16_pulp_hci_setpriority_ne16();
ne16_pulp_hci_set_max_stall(conf->max_stall);
}

void ne16_pulp_close() {
ne16_pulp_hci_reset_max_stall();
ne16_pulp_hci_setpriority_core();
ne16_pulp_cg_disable();
}

void ne16_pulp_event_wait_and_clear() {
eu_evt_maskWaitAndClr(NE16_PULP_EVENT);
}

static const ne16_dev_t ne16_pulp_dev = {
.hwpe_dev = (struct hwpe_dev_t){
.base_addr = (volatile uint32_t *)NE16_PULP_BASE_ADDR}};

const ne16_dev_t *ne16_pulp_get_dev() { return &ne16_pulp_dev; }
81 changes: 81 additions & 0 deletions ne16/bsp/ne16_pulp_bsp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Luka Macan <[email protected]>
*
* Copyright 2023 ETH Zurich and University of Bologna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __NE16_PULP_BSP_H__
#define __NE16_PULP_BSP_H__

#include "ne16.h"
#include <stdint.h>

/**
* ne16_pulp_cg_enable
*
* Enable clock gating of the NE16.
*/
void ne16_pulp_cg_enable();

/**
* ne16_pulp_cg_enable
*
* Disable clock gating of the NE16.
*/
void ne16_pulp_cg_disable();

/**
* ne16_pulp_setpriority_ne16
*
* Set HCI interconnect bus priority to prioritize NE16.
*/
void ne16_pulp_hci_setpriority_ne16();

/**
* ne16_pulp_setpriority_core
*
* Set HCI bus priority to prioritize cores.
*/
void ne16_pulp_hci_setpriority_core();

/**
* ne16_pulp_hci_reset_maxstall
*
* Reset the HCI bus maxstall parameter.
* TODO: Check if it disables it also or just resets?
*/
void ne16_pulp_hci_reset_max_stall();

/**
* ne16_pulp_hci_set_maxstall
*
* Set the HCI bus maxstall. Maxstall defines how many cycles
* will the HCI bus stall the lower priority master, i.e. ne16 or core,
* before letting it do a transaction.
*/
void ne16_pulp_hci_set_max_stall(uint32_t max_stall);

typedef struct ne16_pulp_conf_t {
int max_stall;
} ne16_pulp_conf_t;

void ne16_pulp_open(ne16_pulp_conf_t *conf);
void ne16_pulp_close();
void ne16_pulp_event_wait_and_clear();
const ne16_dev_t *ne16_pulp_get_dev();

#endif // !__NE16_PULP_BSP_H__
Loading