1 // Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "freertos/FreeRTOS.h"
16 #include "freertos/semphr.h"
17 #include <stdatomic.h>
18 #include "sdkconfig.h"
19 #include "spi_common_internal.h"
20 #include "esp_intr_alloc.h"
21 #include "soc/soc_caps.h"
22 #include "stdatomic.h"
23 #include "esp_log.h"
24 #include <strings.h>
25 #include "esp_heap_caps.h"
26 
27 
28 /*
29  * This lock is designed to solve the conflicts between SPI devices (used in tasks) and
30  * the background operations (ISR or cache access).
31  *
32  * There are N (device/task) + 1 (BG) acquiring processer candidates that may touch the bus.
33  *
34  * The core of the lock is a `status` atomic variable, which is always available. No intermediate
35  * status is allowed. The atomic operations (mainly `atomic_fetch_and`, `atomic_fetch_or`)
36  * atomically read the status, and bitwisely write status value ORed / ANDed with given masks.
37  *
38  * Definitions of the status:
39  * - [30]    WEAK_BG_FLAG, active when the BG is the cache
40  * - [29:20] LOCK bits, active when corresponding device is asking for acquiring
41  * - [19:10] PENDING bits, active when the BG acknowledges the REQ bits, but hasn't fully handled them.
42  * - [ 9: 0] REQ bits, active when corresponding device is requesting for BG operations.
43  *
44  *   The REQ bits together PENDING bits are called BG bits, which represent the actual BG request
45  *   state of devices. Either one of REQ or PENDING being active indicates the device has pending BG
46  *   requests. Reason of having two bits instead of one is in the appendix below.
47  *
48  * Acquiring processer means the current processor (task or ISR) allowed to touch the critical
49  * resources, or the SPI bus.
50  *
51  * States of the lock:
52  * - STATE_IDLE: There's no acquiring processor. No device is acquiring the bus, and no BG
53  *   operation is in progress.
54  *
55  * - STATE_ACQ: The acquiring processor is a device task. This means one of the devices is
56  *   acquiring the bus.
57  *
58  * - STATE_BG: The acquiring processor is the ISR, and there is no acquiring device.
59  *
60  * - STATE_BG_ACQ: The acquiring processor is the ISR, and there is an acquiring device.
61  *
62  *
63  * Whenever a bit is written to the status, it means the a device on a task is trying to acquire
64  * the lock (either for the task, or the ISR). When there is no LOCK bits or BG bits active, the
65  * caller immediately become the acquiring processor. Otherwise, the task has to block, and the ISR
66  * will not be invoked until scheduled by the current acquiring processor.
67  *
68  * The acquiring processor is responsible to assign the next acquiring processor by calling the
69  * scheduler, usually after it finishes some requests, and cleared the corresponding status bit.
70  * But there is one exception, when the last bit is cleared from the status, after which there is
71  * no other LOCK bits or BG bits active, the acquiring processor lost its role immediately, and
72  * don't need to call the scheduler to assign the next acquiring processor.
73  *
74  * The acquiring processor may also choose to assign a new acquiring device when there is no, by
75  * calling `spi_bus_lock_bg_rotate_acq_dev` in the ISR. But the acquiring processor, in this case,
76  * is still the ISR, until it calls the scheduler.
77  *
78  *
79  * Transition of the FSM:
80  *
81  * - STATE_IDLE: no acquiring device, nor acquiring processor, no LOCK or BG bits active
82  *   -> STATE_BG: by `req_core`
83  *   -> STATE_ACQ: by `acquire_core`
84  *
85  * - STATE_BG:
86  *      * No acquiring device, the ISR is the acquiring processor, there is BG bits active, but no LOCK
87  *        bits
88  *      * The BG operation should be enabled while turning into this state.
89  *
90  *   -> STATE_IDLE: by `bg_exit_core` after `clear_pend_core` for all BG bits
91  *   -> STATE_BG_ACQ: by `schedule_core`, when there is new LOCK bit set (by `acquire_core`)
92  *
93  * - STATE_BG_ACQ:
94  *      * There is acquiring device, the ISR is the acquiring processor, there may be BG bits active for
95  *        the acquiring device.
96  *      * The BG operation should be enabled while turning into this state.
97  *
98  *   -> STATE_ACQ: by `bg_exit_core` after `clear_pend_core` for all BG bits for the acquiring
99  *                 device.
100  *
101  *                 Should not go to the STATE_ACQ (unblock the acquiring task) until all requests of the
102  *                 acquiring device are finished. This is to preserve the sequence of foreground (polling) and
103  *                 background operations of the device. The background operations queued before the acquiring
104  *                 should be completed first.
105  *
106  * - STATE_ACQ:
107  *      * There is acquiring device, the task is the acquiring processor, there is no BG bits active for
108  *        the acquiring device.
109  *      * The acquiring task (if blocked at `spi_bus_lock_acquire_start` or `spi_bus_lock_wait_bg_done`)
110  *        should be resumed while turning into this state.
111  *
112  *   -> STATE_BG_ACQ: by `req_core`
113  *   -> STATE_BG_ACQ (other device): by `acquire_end_core`, when there is LOCK bit for another
114  *                    device, and the new acquiring device has active BG bits.
115  *   -> STATE_ACQ (other device): by `acquire_end_core`, when there is LOCK bit for another devices,
116  *                    but the new acquiring device has no active BG bits.
117  *   -> STATE_BG: by `acquire_end_core` when there is no LOCK bit active, but there are active BG
118  *                bits.
119  *   -> STATE_IDLE: by `acquire_end_core` when there is no LOCK bit, nor BG bit active.
120  *
121  * The `req_core` used in the task is a little special. It asks for acquiring processor for the
122  * ISR. When it succeed for the first time, it will invoke the ISR (hence passing the acquiring
123  * role to the BG). Otherwise it will not block, the ISR will be automatically be invoked by other
124  * acquiring processor. The caller of `req_core` will never become acquiring processor by this
125  * function.
126  *
127  *
128  * Appendix: The design, that having both request bit and pending bit, is to solve the
129  * concurrency issue between tasks and the bg, when the task can queue several requests,
130  * however the request bit cannot represent the number of requests queued.
131  *
132  * Here's the workflow of task and ISR work concurrently:
133  * - Task: (a) Write to Queue -> (b) Write request bit
134  *   The Task have to write request bit (b) after the data is prepared in the queue (a),
135  *   otherwise the BG may fail to read from the queue when it sees the request bit set.
136  *
137  * - BG: (c) Read queue -> (d) Clear request bit
138  *   Since the BG cannot know the number of requests queued, it have to repeatedly check the
139  *   queue (c), until it find the data is empty, and then clear the request bit (d).
140  *
141  * The events are possible to happen in the order: (c) -> (a) -> (b) -> (d). This may cause a false
142  * clear of the request bit. And there will be data prepared in the queue, but the request bit is
143  * inactive.
144  *
145  * (e) move REQ bits to PEND bits, happen before (c) is introduced to solve this problem. In this
146  * case (d) is changed to clear the PEND bit. Even if (e) -> (c) -> (a) -> (b) -> (d), only PEND
147  * bit is cleared, while the REQ bit is still active.
148  */
149 
150 struct spi_bus_lock_dev_t;
151 typedef struct spi_bus_lock_dev_t spi_bus_lock_dev_t;
152 
153 typedef struct spi_bus_lock_t spi_bus_lock_t;
154 
155 
156 #define MAX_DEV_NUM     10
157 
158 // Bit 29-20: lock bits, Bit 19-10: pending bits
159 // Bit 9-0: request bits, Bit 30:
160 #define LOCK_SHIFT      20
161 #define PENDING_SHIFT   10
162 #define REQ_SHIFT       0
163 
164 #define WEAK_BG_FLAG    BIT(30)    /**< The bus is permanently requested by background operations.
165                                      * This flag is weak, will not prevent acquiring of devices. But will help the BG to be re-enabled again after the bus is release.
166                                      */
167 
168 // get the bit mask wher bit [high-1, low] are all 1'b1 s.
169 #define BIT1_MASK(high, low)   ((UINT32_MAX << (high)) ^ (UINT32_MAX << (low)))
170 
171 #define LOCK_BIT(mask)      ((mask) << LOCK_SHIFT)
172 #define REQUEST_BIT(mask)   ((mask) << REQ_SHIFT)
173 #define PENDING_BIT(mask)   ((mask) << PENDING_SHIFT)
174 #define DEV_MASK(id)        (LOCK_BIT(1<<id) | PENDING_BIT(1<<id) | REQUEST_BIT(1<<id))
175 #define ID_DEV_MASK(mask)   (ffs(mask) - 1)
176 
177 #define REQ_MASK            BIT1_MASK(REQ_SHIFT+MAX_DEV_NUM, REQ_SHIFT)
178 #define PEND_MASK           BIT1_MASK(PENDING_SHIFT+MAX_DEV_NUM, PENDING_SHIFT)
179 #define BG_MASK             BIT1_MASK(REQ_SHIFT+MAX_DEV_NUM*2, REQ_SHIFT)
180 #define LOCK_MASK           BIT1_MASK(LOCK_SHIFT+MAX_DEV_NUM, LOCK_SHIFT)
181 
182 #define DEV_REQ_MASK(dev)   ((dev)->mask & REQ_MASK)
183 #define DEV_PEND_MASK(dev)  ((dev)->mask & PEND_MASK)
184 #define DEV_BG_MASK(dev)    ((dev)->mask & BG_MASK)
185 
186 struct spi_bus_lock_t {
187     /**
188      * The core of the lock. These bits are status of the lock, which should be always available.
189      * No intermediate status is allowed. This is realized by atomic operations, mainly
190      * `atomic_fetch_and`, `atomic_fetch_or`, which atomically read the status, and bitwise write
191      * status value ORed / ANDed with given masks.
192      *
193      * The request bits together pending bits represent the actual bg request state of one device.
194      * Either one of them being active indicates the device has pending bg requests.
195      *
196      * Whenever a bit is written to the status, it means the a device on a task is trying to
197      * acquire the lock. But this will succeed only when no LOCK or BG bits active.
198      *
199      * The acquiring processor is responsible to call the scheduler to pass its role to other tasks
200      * or the BG, unless it clear the last bit in the status register.
201      */
202     //// Critical resources, they are only writable by acquiring processor, and stable only when read by the acquiring processor.
203     atomic_uint_fast32_t    status;
204     spi_bus_lock_dev_t* volatile acquiring_dev;   ///< The acquiring device
205     bool                volatile acq_dev_bg_active;    ///< BG is the acquiring processor serving the acquiring device, used for the wait_bg to skip waiting quickly.
206     bool                volatile in_isr;         ///< ISR is touching HW
207     //// End of critical resources
208 
209     atomic_intptr_t     dev[DEV_NUM_MAX];     ///< Child locks.
210     bg_ctrl_func_t      bg_enable;      ///< Function to enable background operations.
211     bg_ctrl_func_t      bg_disable;     ///< Function to disable background operations
212     void*               bg_arg;            ///< Argument for `bg_enable` and `bg_disable` functions.
213 
214     spi_bus_lock_dev_t* last_dev;       ///< Last used device, to decide whether to refresh all registers.
215     int                 periph_cs_num;  ///< Number of the CS pins the HW has.
216 
217     //debug information
218     int                 host_id;        ///< Host ID, for debug information printing
219     uint32_t            new_req;        ///< Last int_req when `spi_bus_lock_bg_start` is called. Debug use.
220 };
221 
222 struct spi_bus_lock_dev_t {
223     SemaphoreHandle_t   semphr;     ///< Binray semaphore to notify the device it claimed the bus
224     spi_bus_lock_t*     parent;     ///< Pointer to parent spi_bus_lock_t
225     uint32_t            mask;       ///< Bitwise OR-ed mask of the REQ, PEND, LOCK bits of this device
226 };
227 
228 DRAM_ATTR static const char TAG[] = "bus_lock";
229 
230 #define LOCK_CHECK(a, str, ret_val, ...) \
231     if (!(a)) { \
232         ESP_LOGE(TAG,"%s(%d): "str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
233         return (ret_val); \
234     }
235 
236 static inline int mask_get_id(uint32_t mask);
237 static inline int dev_lock_get_id(spi_bus_lock_dev_t *dev_lock);
238 
239 /*******************************************************************************
240  * atomic operations to the status
241  ******************************************************************************/
lock_status_fetch_set(spi_bus_lock_t * lock,uint32_t set)242 SPI_MASTER_ISR_ATTR static inline uint32_t lock_status_fetch_set(spi_bus_lock_t *lock, uint32_t set)
243 {
244     return atomic_fetch_or(&lock->status, set);
245 }
246 
lock_status_fetch_clear(spi_bus_lock_t * lock,uint32_t clear)247 IRAM_ATTR static inline uint32_t lock_status_fetch_clear(spi_bus_lock_t *lock, uint32_t clear)
248 {
249     return atomic_fetch_and(&lock->status, ~clear);
250 }
251 
lock_status_fetch(spi_bus_lock_t * lock)252 IRAM_ATTR static inline uint32_t lock_status_fetch(spi_bus_lock_t *lock)
253 {
254     return atomic_load(&lock->status);
255 }
256 
lock_status_init(spi_bus_lock_t * lock)257 SPI_MASTER_ISR_ATTR static inline void lock_status_init(spi_bus_lock_t *lock)
258 {
259     atomic_store(&lock->status, 0);
260 }
261 
262 // return the remaining status bits
lock_status_clear(spi_bus_lock_t * lock,uint32_t clear)263 IRAM_ATTR static inline uint32_t lock_status_clear(spi_bus_lock_t* lock, uint32_t clear)
264 {
265     //the fetch and clear should be atomic, avoid missing the all '0' status when all bits are clear.
266     uint32_t state = lock_status_fetch_clear(lock, clear);
267     return state & (~clear);
268 }
269 
270 /*******************************************************************************
271  * Schedule service
272  *
273  * The modification to the status bits may cause rotating of the acquiring processor. It also have
274  * effects to `acquired_dev` (the acquiring device), `in_isr` (HW used in BG), and
275  * `acq_dev_bg_active` (wait_bg_end can be skipped) members of the lock structure.
276  *
277  * Most of them should be atomic, and special attention should be paid to the operation
278  * sequence.
279  ******************************************************************************/
resume_dev_in_isr(spi_bus_lock_dev_t * dev_lock,BaseType_t * do_yield)280 SPI_MASTER_ISR_ATTR static inline void resume_dev_in_isr(spi_bus_lock_dev_t *dev_lock, BaseType_t *do_yield)
281 {
282     xSemaphoreGiveFromISR(dev_lock->semphr, do_yield);
283 }
284 
resume_dev(const spi_bus_lock_dev_t * dev_lock)285 IRAM_ATTR static inline void resume_dev(const spi_bus_lock_dev_t *dev_lock)
286 {
287     xSemaphoreGive(dev_lock->semphr);
288 }
289 
bg_disable(spi_bus_lock_t * lock)290 SPI_MASTER_ISR_ATTR static inline void bg_disable(spi_bus_lock_t *lock)
291 {
292     BUS_LOCK_DEBUG_EXECUTE_CHECK(lock->bg_disable);
293     lock->bg_disable(lock->bg_arg);
294 }
295 
bg_enable(spi_bus_lock_t * lock)296 IRAM_ATTR static inline void bg_enable(spi_bus_lock_t* lock)
297 {
298     BUS_LOCK_DEBUG_EXECUTE_CHECK(lock->bg_enable);
299     lock->bg_enable(lock->bg_arg);
300 }
301 
302 // Set the REQ bit. If we become the acquiring processor, invoke the ISR and pass that to it.
303 // The caller will never become the acquiring processor after this function returns.
req_core(spi_bus_lock_dev_t * dev_handle)304 SPI_MASTER_ATTR static inline void req_core(spi_bus_lock_dev_t *dev_handle)
305 {
306     spi_bus_lock_t *lock = dev_handle->parent;
307 
308     // Though `acquired_dev` is critical resource, `dev_handle == lock->acquired_dev`
309     // is a stable statement unless `acquire_start` or `acquire_end` is called by current
310     // device.
311     if (dev_handle == lock->acquiring_dev){
312         // Set the REQ bit and check BG bits if we are the acquiring processor.
313         // If the BG bits were not active before, invoke the BG again.
314 
315         // Avoid competitive risk against the `clear_pend_core`, `acq_dev_bg_active` should be set before
316         // setting REQ bit.
317         lock->acq_dev_bg_active = true;
318         uint32_t status = lock_status_fetch_set(lock, DEV_REQ_MASK(dev_handle));
319         if ((status & DEV_BG_MASK(dev_handle)) == 0) {
320             bg_enable(lock); //acquiring processor passed to BG
321         }
322     } else {
323         uint32_t status = lock_status_fetch_set(lock, DEV_REQ_MASK(dev_handle));
324         if (status == 0) {
325             bg_enable(lock); //acquiring processor passed to BG
326         }
327     }
328 }
329 
330 //Set the LOCK bit. Handle related stuff and return true if we become the acquiring processor.
acquire_core(spi_bus_lock_dev_t * dev_handle)331 SPI_MASTER_ISR_ATTR static inline bool acquire_core(spi_bus_lock_dev_t *dev_handle)
332 {
333     spi_bus_lock_t* lock = dev_handle->parent;
334     uint32_t status = lock_status_fetch_set(lock, dev_handle->mask & LOCK_MASK);
335 
336     // Check all bits except WEAK_BG
337     if ((status & (BG_MASK | LOCK_MASK)) == 0) {
338         //succeed at once
339         lock->acquiring_dev = dev_handle;
340         BUS_LOCK_DEBUG_EXECUTE_CHECK(!lock->acq_dev_bg_active);
341         if (status & WEAK_BG_FLAG) {
342             //Mainly to disable the cache (Weak_BG), that is not able to disable itself
343             bg_disable(lock);
344         }
345         return true;
346     }
347     return false;
348 }
349 
350 /**
351  * Find the next acquiring processor according to the status. Will directly change
352  * the acquiring device if new one found.
353  *
354  * Cases:
355  * - BG should still be the acquiring processor (Return false):
356  *     1. Acquiring device has active BG bits: out_desired_dev = new acquiring device
357  *     2. No acquiring device, but BG active: out_desired_dev = randomly pick one device with active BG bits
358  * - BG should yield to the task (Return true):
359  *     3. Acquiring device has no active BG bits: out_desired_dev = new acquiring device
360  *     4. No acquiring device while no active BG bits: out_desired_dev=NULL
361  *
362  * Acquiring device task need to be resumed only when case 3.
363  *
364  * This scheduling can happen in either task or ISR, so `in_isr` or `bg_active` not touched.
365  *
366  * @param lock
367  * @param status Current status
368  * @param out_desired_dev Desired device to work next, see above.
369  *
370  * @return False if BG should still be the acquiring processor, otherwise True (yield to task).
371  */
372 IRAM_ATTR static inline bool
schedule_core(spi_bus_lock_t * lock,uint32_t status,spi_bus_lock_dev_t ** out_desired_dev)373 schedule_core(spi_bus_lock_t *lock, uint32_t status, spi_bus_lock_dev_t **out_desired_dev)
374 {
375     spi_bus_lock_dev_t* desired_dev = NULL;
376     uint32_t lock_bits = (status & LOCK_MASK) >> LOCK_SHIFT;
377     uint32_t bg_bits = status & BG_MASK;
378     bg_bits = ((bg_bits >> REQ_SHIFT) | (bg_bits >> PENDING_SHIFT)) & REQ_MASK;
379 
380     bool bg_yield;
381     if (lock_bits) {
382         int dev_id = mask_get_id(lock_bits);
383         desired_dev = (spi_bus_lock_dev_t *)atomic_load(&lock->dev[dev_id]);
384         BUS_LOCK_DEBUG_EXECUTE_CHECK(desired_dev);
385 
386         lock->acquiring_dev = desired_dev;
387         bg_yield = ((bg_bits & desired_dev->mask) == 0);
388         lock->acq_dev_bg_active = !bg_yield;
389     } else {
390         lock->acq_dev_bg_active = false;
391         if (bg_bits) {
392             int dev_id = mask_get_id(bg_bits);
393             desired_dev = (spi_bus_lock_dev_t *)atomic_load(&lock->dev[dev_id]);
394             BUS_LOCK_DEBUG_EXECUTE_CHECK(desired_dev);
395 
396             lock->acquiring_dev = NULL;
397             bg_yield = false;
398         } else {
399             desired_dev = NULL;
400             lock->acquiring_dev = NULL;
401             bg_yield = true;
402         }
403     }
404     *out_desired_dev = desired_dev;
405     return bg_yield;
406 }
407 
408 //Clear the LOCK bit and trigger a rescheduling.
acquire_end_core(spi_bus_lock_dev_t * dev_handle)409 IRAM_ATTR static inline void acquire_end_core(spi_bus_lock_dev_t *dev_handle)
410 {
411     spi_bus_lock_t* lock = dev_handle->parent;
412     uint32_t status = lock_status_clear(lock, dev_handle->mask & LOCK_MASK);
413     spi_bus_lock_dev_t* desired_dev = NULL;
414 
415     bool invoke_bg = !schedule_core(lock, status, &desired_dev);
416     if (invoke_bg) {
417         bg_enable(lock);
418     } else if (desired_dev) {
419         resume_dev(desired_dev);
420     } else if (status & WEAK_BG_FLAG) {
421         bg_enable(lock);
422     }
423 }
424 
425 // Move the REQ bits to corresponding PEND bits. Must be called by acquiring processor.
426 // Have no side effects on the acquiring device/processor.
update_pend_core(spi_bus_lock_t * lock,uint32_t status)427 SPI_MASTER_ISR_ATTR static inline void update_pend_core(spi_bus_lock_t *lock, uint32_t status)
428 {
429     uint32_t active_req_bits = status & REQ_MASK;
430 #if PENDING_SHIFT > REQ_SHIFT
431     uint32_t pending_mask = active_req_bits << (PENDING_SHIFT - REQ_SHIFT);
432 #else
433     uint32_t pending_mask = active_req_bits >> (REQ_SHIFT - PENDING_SHIFT);
434 #endif
435     // We have to set the PEND bits and then clear the REQ bits, since BG bits are using bitwise OR logic,
436     // this will not influence the effectiveness of the BG bits of every device.
437     lock_status_fetch_set(lock, pending_mask);
438     lock_status_fetch_clear(lock, active_req_bits);
439 }
440 
441 // Clear the PEND bit (not REQ bit!) of a device, return the suggestion whether we can try to quit the ISR.
442 // Lost the acquiring processor immediately when the BG bits for active device are inactive, indiciating by the return value.
443 // Can be called only when ISR is acting as the acquiring processor.
clear_pend_core(spi_bus_lock_dev_t * dev_handle)444 SPI_MASTER_ISR_ATTR static inline bool clear_pend_core(spi_bus_lock_dev_t *dev_handle)
445 {
446     bool finished;
447     spi_bus_lock_t *lock = dev_handle->parent;
448     uint32_t pend_mask = DEV_PEND_MASK(dev_handle);
449     BUS_LOCK_DEBUG_EXECUTE_CHECK(lock_status_fetch(lock) & pend_mask);
450 
451     uint32_t status = lock_status_clear(lock, pend_mask);
452 
453     if (lock->acquiring_dev == dev_handle) {
454         finished = ((status & DEV_REQ_MASK(dev_handle)) == 0);
455         if (finished) {
456             lock->acq_dev_bg_active = false;
457         }
458     } else {
459         finished = (status == 0);
460     }
461     return finished;
462 }
463 
464 // Return true if the ISR has already touched the HW, which means previous operations should
465 // be terminated first, before we use the HW again. Otherwise return false.
466 // In either case `in_isr` will be marked as true, until call to `bg_exit_core` with `wip=false` successfully.
bg_entry_core(spi_bus_lock_t * lock)467 SPI_MASTER_ISR_ATTR static inline bool bg_entry_core(spi_bus_lock_t *lock)
468 {
469     BUS_LOCK_DEBUG_EXECUTE_CHECK(!lock->acquiring_dev || lock->acq_dev_bg_active);
470     /*
471      * The interrupt is disabled at the entry of ISR to avoid competitive risk as below:
472      *
473      * The `esp_intr_enable` will be called (b) after new BG request is queued (a) in the task;
474      * while `esp_intr_disable` should be called (c) if we check and found the sending queue is empty (d).
475      * If (c) happens after (d), if things happens in this sequence:
476      * (d) -> (a) -> (b) -> (c), the interrupt will be disabled while there's pending BG request in the queue.
477      *
478      * To avoid this, interrupt is disabled here, and re-enabled later if required. (c) -> (d) -> (a) -> (b) -> revert (c) if !d
479      */
480     bg_disable(lock);
481     if (lock->in_isr) {
482         return false;
483     } else {
484         lock->in_isr = true;
485         return true;
486     }
487 }
488 
489 // Handle the conditions of status and interrupt, avoiding the ISR being disabled when there is any new coming BG requests.
490 // When called with `wip=true`, means the ISR is performing some operations. Will enable the interrupt again and exit unconditionally.
491 // When called with `wip=false`, will only return `true` when there is no coming BG request. If return value is `false`, the ISR should try again.
492 // Will not change acquiring device.
bg_exit_core(spi_bus_lock_t * lock,bool wip,BaseType_t * do_yield)493 SPI_MASTER_ISR_ATTR static inline bool bg_exit_core(spi_bus_lock_t *lock, bool wip, BaseType_t *do_yield)
494 {
495     //See comments in `bg_entry_core`, re-enable interrupt disabled in entry if we do need the interrupt
496     if (wip) {
497         bg_enable(lock);
498         BUS_LOCK_DEBUG_EXECUTE_CHECK(!lock->acquiring_dev || lock->acq_dev_bg_active);
499         return true;
500     }
501 
502     bool ret;
503     uint32_t status = lock_status_fetch(lock);
504     if (lock->acquiring_dev) {
505         if (status & DEV_BG_MASK(lock->acquiring_dev)) {
506             BUS_LOCK_DEBUG_EXECUTE_CHECK(lock->acq_dev_bg_active);
507             ret = false;
508         } else {
509             // The request may happen any time, even after we fetched the status.
510             // The value of `acq_dev_bg_active` is random.
511             resume_dev_in_isr(lock->acquiring_dev, do_yield);
512             ret = true;
513         }
514     } else {
515         BUS_LOCK_DEBUG_EXECUTE_CHECK(!lock->acq_dev_bg_active);
516         ret = !(status & BG_MASK);
517     }
518     if (ret) {
519         //when successfully exit, but no transaction done, mark BG as inactive
520         lock->in_isr = false;
521     }
522     return ret;
523 }
524 
dev_wait_prepare(spi_bus_lock_dev_t * dev_handle)525 IRAM_ATTR static inline void dev_wait_prepare(spi_bus_lock_dev_t *dev_handle)
526 {
527     xSemaphoreTake(dev_handle->semphr, 0);
528 }
529 
dev_wait(spi_bus_lock_dev_t * dev_handle,TickType_t wait)530 SPI_MASTER_ISR_ATTR static inline esp_err_t dev_wait(spi_bus_lock_dev_t *dev_handle, TickType_t wait)
531 {
532     BaseType_t ret = xSemaphoreTake(dev_handle->semphr, wait);
533 
534     if (ret == pdFALSE) return ESP_ERR_TIMEOUT;
535     return ESP_OK;
536 }
537 
538 /*******************************************************************************
539  * Initialization & Deinitialization
540  ******************************************************************************/
spi_bus_init_lock(spi_bus_lock_handle_t * out_lock,const spi_bus_lock_config_t * config)541 esp_err_t spi_bus_init_lock(spi_bus_lock_handle_t *out_lock, const spi_bus_lock_config_t *config)
542 {
543     spi_bus_lock_t* lock = (spi_bus_lock_t*)calloc(sizeof(spi_bus_lock_t), 1);
544     if (lock == NULL) {
545         return ESP_ERR_NO_MEM;
546     }
547 
548     lock_status_init(lock);
549     lock->acquiring_dev = NULL;
550     lock->last_dev = NULL;
551     lock->periph_cs_num = config->cs_num;
552     lock->host_id = config->host_id;
553 
554     *out_lock = lock;
555     return ESP_OK;
556 }
557 
spi_bus_deinit_lock(spi_bus_lock_handle_t lock)558 void spi_bus_deinit_lock(spi_bus_lock_handle_t lock)
559 {
560     for (int i = 0; i < DEV_NUM_MAX; i++) {
561         assert(atomic_load(&lock->dev[i]) == (intptr_t)NULL);
562     }
563     free(lock);
564 }
565 
try_acquire_free_dev(spi_bus_lock_t * lock,bool cs_required)566 static int try_acquire_free_dev(spi_bus_lock_t *lock, bool cs_required)
567 {
568     if (cs_required) {
569         int i;
570         for (i = 0; i < lock->periph_cs_num; i++) {
571             intptr_t null = (intptr_t) NULL;
572             //use 1 to occupy the slot, actual setup comes later
573             if (atomic_compare_exchange_strong(&lock->dev[i], &null, (intptr_t) 1)) {
574                 break;
575             }
576         }
577         return ((i == lock->periph_cs_num)? -1: i);
578     } else {
579         int i;
580         for (i = DEV_NUM_MAX - 1; i >= 0; i--) {
581             intptr_t null = (intptr_t) NULL;
582             //use 1 to occupy the slot, actual setup comes later
583             if (atomic_compare_exchange_strong(&lock->dev[i], &null, (intptr_t) 1)) {
584                 break;
585             }
586         }
587         return i;
588     }
589 }
590 
spi_bus_lock_register_dev(spi_bus_lock_handle_t lock,spi_bus_lock_dev_config_t * config,spi_bus_lock_dev_handle_t * out_dev_handle)591 esp_err_t spi_bus_lock_register_dev(spi_bus_lock_handle_t lock, spi_bus_lock_dev_config_t *config,
592                                     spi_bus_lock_dev_handle_t *out_dev_handle)
593 {
594     if (lock == NULL) return ESP_ERR_INVALID_ARG;
595     int id = try_acquire_free_dev(lock, config->flags & SPI_BUS_LOCK_DEV_FLAG_CS_REQUIRED);
596     if (id == -1) return ESP_ERR_NOT_SUPPORTED;
597 
598     spi_bus_lock_dev_t* dev_lock = (spi_bus_lock_dev_t*)heap_caps_calloc(sizeof(spi_bus_lock_dev_t), 1, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
599     if (dev_lock == NULL) {
600         return ESP_ERR_NO_MEM;
601     }
602     dev_lock->semphr = xSemaphoreCreateBinary();
603     if (dev_lock->semphr == NULL) {
604         free(dev_lock);
605         atomic_store(&lock->dev[id], (intptr_t)NULL);
606         return ESP_ERR_NO_MEM;
607     }
608     dev_lock->parent = lock;
609     dev_lock->mask = DEV_MASK(id);
610 
611     ESP_LOGV(TAG, "device registered on bus %d slot %d.", lock->host_id, id);
612     atomic_store(&lock->dev[id], (intptr_t)dev_lock);
613     *out_dev_handle = dev_lock;
614     return ESP_OK;
615 }
616 
spi_bus_lock_unregister_dev(spi_bus_lock_dev_handle_t dev_handle)617 void spi_bus_lock_unregister_dev(spi_bus_lock_dev_handle_t dev_handle)
618 {
619     int id = dev_lock_get_id(dev_handle);
620 
621     spi_bus_lock_t* lock = dev_handle->parent;
622     BUS_LOCK_DEBUG_EXECUTE_CHECK(atomic_load(&lock->dev[id]) == (intptr_t)dev_handle);
623 
624     if (lock->last_dev == dev_handle) lock->last_dev = NULL;
625 
626     atomic_store(&lock->dev[id], (intptr_t)NULL);
627     if (dev_handle->semphr) {
628         vSemaphoreDelete(dev_handle->semphr);
629     }
630 
631     free(dev_handle);
632 }
633 
mask_get_id(uint32_t mask)634 IRAM_ATTR static inline int mask_get_id(uint32_t mask)
635 {
636     return ID_DEV_MASK(mask);
637 }
638 
dev_lock_get_id(spi_bus_lock_dev_t * dev_lock)639 IRAM_ATTR static inline int dev_lock_get_id(spi_bus_lock_dev_t *dev_lock)
640 {
641     return mask_get_id(dev_lock->mask);
642 }
643 
spi_bus_lock_set_bg_control(spi_bus_lock_handle_t lock,bg_ctrl_func_t bg_enable,bg_ctrl_func_t bg_disable,void * arg)644 void spi_bus_lock_set_bg_control(spi_bus_lock_handle_t lock, bg_ctrl_func_t bg_enable, bg_ctrl_func_t bg_disable, void *arg)
645 {
646     lock->bg_enable = bg_enable;
647     lock->bg_disable = bg_disable;
648     lock->bg_arg = arg;
649 }
650 
spi_bus_lock_get_dev_id(spi_bus_lock_dev_handle_t dev_handle)651 IRAM_ATTR int spi_bus_lock_get_dev_id(spi_bus_lock_dev_handle_t dev_handle)
652 {
653     return (dev_handle? dev_lock_get_id(dev_handle): -1);
654 }
655 
656 //will be called when cache disabled
spi_bus_lock_touch(spi_bus_lock_dev_handle_t dev_handle)657 IRAM_ATTR bool spi_bus_lock_touch(spi_bus_lock_dev_handle_t dev_handle)
658 {
659     spi_bus_lock_dev_t* last_dev = dev_handle->parent->last_dev;
660     dev_handle->parent->last_dev = dev_handle;
661     if (last_dev != dev_handle) {
662         int last_dev_id = (last_dev? dev_lock_get_id(last_dev): -1);
663         ESP_DRAM_LOGV(TAG, "SPI dev changed from %d to %d",
664                     last_dev_id, dev_lock_get_id(dev_handle));
665     }
666     return (dev_handle != last_dev);
667 }
668 
669 /*******************************************************************************
670  * Acquiring service
671  ******************************************************************************/
spi_bus_lock_acquire_start(spi_bus_lock_dev_t * dev_handle,TickType_t wait)672 IRAM_ATTR esp_err_t spi_bus_lock_acquire_start(spi_bus_lock_dev_t *dev_handle, TickType_t wait)
673 {
674     LOCK_CHECK(wait == portMAX_DELAY, "timeout other than portMAX_DELAY not supported", ESP_ERR_INVALID_ARG);
675 
676     spi_bus_lock_t* lock = dev_handle->parent;
677 
678     // Clear the semaphore before checking
679     dev_wait_prepare(dev_handle);
680     if (!acquire_core(dev_handle)) {
681         //block until becoming the acquiring processor (help by previous acquiring processor)
682         esp_err_t err = dev_wait(dev_handle, wait);
683         //TODO: add timeout handling here.
684         if (err != ESP_OK) return err;
685     }
686 
687     ESP_DRAM_LOGV(TAG, "dev %d acquired.", dev_lock_get_id(dev_handle));
688     BUS_LOCK_DEBUG_EXECUTE_CHECK(lock->acquiring_dev == dev_handle);
689 
690     //When arrives at here, requests of this device should already be handled
691     uint32_t status = lock_status_fetch(lock);
692     (void) status;
693     BUS_LOCK_DEBUG_EXECUTE_CHECK((status & DEV_BG_MASK(dev_handle)) == 0);
694 
695     return ESP_OK;
696 }
697 
spi_bus_lock_acquire_end(spi_bus_lock_dev_t * dev_handle)698 IRAM_ATTR esp_err_t spi_bus_lock_acquire_end(spi_bus_lock_dev_t *dev_handle)
699 {
700     //release the bus
701     spi_bus_lock_t* lock = dev_handle->parent;
702     LOCK_CHECK(lock->acquiring_dev == dev_handle, "Cannot release a lock that hasn't been acquired.", ESP_ERR_INVALID_STATE);
703 
704     acquire_end_core(dev_handle);
705 
706     ESP_LOGV(TAG, "dev %d released.", dev_lock_get_id(dev_handle));
707     return ESP_OK;
708 }
709 
spi_bus_lock_get_acquiring_dev(spi_bus_lock_t * lock)710 SPI_MASTER_ISR_ATTR spi_bus_lock_dev_handle_t spi_bus_lock_get_acquiring_dev(spi_bus_lock_t *lock)
711 {
712     return lock->acquiring_dev;
713 }
714 
715 /*******************************************************************************
716  * BG (background operation) service
717  ******************************************************************************/
spi_bus_lock_bg_entry(spi_bus_lock_t * lock)718 SPI_MASTER_ISR_ATTR bool spi_bus_lock_bg_entry(spi_bus_lock_t* lock)
719 {
720     return bg_entry_core(lock);
721 }
722 
spi_bus_lock_bg_exit(spi_bus_lock_t * lock,bool wip,BaseType_t * do_yield)723 SPI_MASTER_ISR_ATTR bool spi_bus_lock_bg_exit(spi_bus_lock_t* lock, bool wip, BaseType_t* do_yield)
724 {
725     return bg_exit_core(lock, wip, do_yield);
726 }
727 
spi_bus_lock_bg_request(spi_bus_lock_dev_t * dev_handle)728 SPI_MASTER_ATTR esp_err_t spi_bus_lock_bg_request(spi_bus_lock_dev_t *dev_handle)
729 {
730     req_core(dev_handle);
731     return ESP_OK;
732 }
733 
spi_bus_lock_wait_bg_done(spi_bus_lock_dev_handle_t dev_handle,TickType_t wait)734 IRAM_ATTR esp_err_t spi_bus_lock_wait_bg_done(spi_bus_lock_dev_handle_t dev_handle, TickType_t wait)
735 {
736     spi_bus_lock_t *lock = dev_handle->parent;
737     LOCK_CHECK(lock->acquiring_dev == dev_handle, "Cannot wait for a device that is not acquired", ESP_ERR_INVALID_STATE);
738     LOCK_CHECK(wait == portMAX_DELAY, "timeout other than portMAX_DELAY not supported", ESP_ERR_INVALID_ARG);
739 
740     // If no BG bits active, skip quickly. This is ensured by `spi_bus_lock_wait_bg_done`
741     // cannot be executed with `bg_request` on the same device concurrently.
742     if (lock_status_fetch(lock) & DEV_BG_MASK(dev_handle)) {
743         // Clear the semaphore before checking
744         dev_wait_prepare(dev_handle);
745         if (lock_status_fetch(lock) & DEV_BG_MASK(dev_handle)) {
746             //block until becoming the acquiring processor (help by previous acquiring processor)
747             esp_err_t err = dev_wait(dev_handle, wait);
748             //TODO: add timeout handling here.
749             if (err != ESP_OK) return err;
750         }
751     }
752 
753     BUS_LOCK_DEBUG_EXECUTE_CHECK(!lock->acq_dev_bg_active);
754     BUS_LOCK_DEBUG_EXECUTE_CHECK((lock_status_fetch(lock) & DEV_BG_MASK(dev_handle)) == 0);
755     return ESP_OK;
756 }
757 
spi_bus_lock_bg_clear_req(spi_bus_lock_dev_t * dev_handle)758 SPI_MASTER_ISR_ATTR bool spi_bus_lock_bg_clear_req(spi_bus_lock_dev_t *dev_handle)
759 {
760     bool finished = clear_pend_core(dev_handle);
761     ESP_EARLY_LOGV(TAG, "dev %d served from bg.", dev_lock_get_id(dev_handle));
762     return finished;
763 }
764 
spi_bus_lock_bg_check_dev_acq(spi_bus_lock_t * lock,spi_bus_lock_dev_handle_t * out_dev_lock)765 SPI_MASTER_ISR_ATTR bool spi_bus_lock_bg_check_dev_acq(spi_bus_lock_t *lock,
766                                                        spi_bus_lock_dev_handle_t *out_dev_lock)
767 {
768     BUS_LOCK_DEBUG_EXECUTE_CHECK(!lock->acquiring_dev);
769     uint32_t status = lock_status_fetch(lock);
770     return schedule_core(lock, status, out_dev_lock);
771 }
772 
spi_bus_lock_bg_check_dev_req(spi_bus_lock_dev_t * dev_lock)773 SPI_MASTER_ISR_ATTR bool spi_bus_lock_bg_check_dev_req(spi_bus_lock_dev_t *dev_lock)
774 {
775     spi_bus_lock_t* lock = dev_lock->parent;
776     uint32_t status = lock_status_fetch(lock);
777     uint32_t dev_status = status & dev_lock->mask;
778 
779     // move REQ bits of all device to corresponding PEND bits.
780     // To reduce executing time, only done when the REQ bit of the calling device is set.
781     if (dev_status & REQ_MASK) {
782         update_pend_core(lock, status);
783         return true;
784     } else {
785         return dev_status & PEND_MASK;
786     }
787 }
788 
spi_bus_lock_bg_req_exist(spi_bus_lock_t * lock)789 SPI_MASTER_ISR_ATTR bool spi_bus_lock_bg_req_exist(spi_bus_lock_t *lock)
790 {
791     uint32_t status = lock_status_fetch(lock);
792     return status & BG_MASK;
793 }
794 
795 /*******************************************************************************
796  * Static variables of the locks of the main flash
797  ******************************************************************************/
798 #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS
799 static spi_bus_lock_dev_t lock_main_flash_dev;
800 
801 static spi_bus_lock_t main_spi_bus_lock = {
802     /*
803      * the main bus cache is permanently required, this flag is set here and never clear so that the
804      * cache will always be enabled if acquiring devices yield.
805      */
806     .status = ATOMIC_VAR_INIT(WEAK_BG_FLAG),
807     .acquiring_dev = NULL,
808     .dev = {ATOMIC_VAR_INIT((intptr_t)&lock_main_flash_dev)},
809     .new_req = 0,
810     .periph_cs_num = SOC_SPI_PERIPH_CS_NUM(0),
811 };
812 const spi_bus_lock_handle_t g_main_spi_bus_lock = &main_spi_bus_lock;
813 
spi_bus_lock_init_main_bus(void)814 esp_err_t spi_bus_lock_init_main_bus(void)
815 {
816     spi_bus_main_set_lock(g_main_spi_bus_lock);
817     return ESP_OK;
818 }
819 
820 static StaticSemaphore_t main_flash_semphr;
821 
822 static spi_bus_lock_dev_t lock_main_flash_dev = {
823     .semphr = NULL,
824     .parent = &main_spi_bus_lock,
825     .mask = DEV_MASK(0),
826 };
827 const spi_bus_lock_dev_handle_t g_spi_lock_main_flash_dev = &lock_main_flash_dev;
828 
spi_bus_lock_init_main_dev(void)829 esp_err_t spi_bus_lock_init_main_dev(void)
830 {
831     g_spi_lock_main_flash_dev->semphr = xSemaphoreCreateBinaryStatic(&main_flash_semphr);
832     if (g_spi_lock_main_flash_dev->semphr == NULL) {
833         return ESP_ERR_NO_MEM;
834     }
835     return ESP_OK;
836 }
837 #else //CONFIG_SPI_FLASH_SHARE_SPI1_BUS
838 
839 //when the dev lock is not initialized, point to NULL
840 const spi_bus_lock_dev_handle_t g_spi_lock_main_flash_dev = NULL;
841 
842 #endif
843