14f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta/* 2e83b0cadc67882c1ba7f430d16dab80c9b3a0228Dan Handley * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. 34f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * 44f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * Redistribution and use in source and binary forms, with or without 54f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * modification, are permitted provided that the following conditions are met: 64f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * 74f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * Redistributions of source code must retain the above copyright notice, this 84f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * list of conditions and the following disclaimer. 94f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * 104f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * Redistributions in binary form must reproduce the above copyright notice, 114f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * this list of conditions and the following disclaimer in the documentation 124f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * and/or other materials provided with the distribution. 134f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * 144f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * Neither the name of ARM nor the names of its contributors may be used 154f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * to endorse or promote products derived from this software without specific 164f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * prior written permission. 174f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * 184f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 194f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 204f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 214f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 224f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 234f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 244f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 254f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 264f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 274f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 284f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta * POSSIBILITY OF SUCH DAMAGE. 294f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta */ 304f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta 314f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta#ifndef __BAKERY_LOCK_H__ 324f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta#define __BAKERY_LOCK_H__ 334f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta 345f0cdb059d7d5c3a8a834074a7f236b85d014ddeDan Handley#include <platform_def.h> 354f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta 364f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta#define BAKERY_LOCK_MAX_CPUS PLATFORM_CORE_COUNT 374f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta 388c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew#ifndef __ASSEMBLY__ 398c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew#include <stdint.h> 408c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew 418c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew#if USE_COHERENT_MEM 428c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew 43fb037bfb7cbf7b404c069b4ebac5a10059d948b1Dan Handleytypedef struct bakery_lock { 4482a0aca05394343407f7f12ff454ac9e8cdfcba6Jeenu Viswambharan int owner; 454f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta volatile char entering[BAKERY_LOCK_MAX_CPUS]; 464f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta volatile unsigned number[BAKERY_LOCK_MAX_CPUS]; 47fb037bfb7cbf7b404c069b4ebac5a10059d948b1Dan Handley} bakery_lock_t; 484f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta 494f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta#define NO_OWNER (-1) 504f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta 51fb037bfb7cbf7b404c069b4ebac5a10059d948b1Dan Handleyvoid bakery_lock_init(bakery_lock_t *bakery); 52634ec6c23b4037ec6efc2a594fd6b666339171e9Andrew Thoelkevoid bakery_lock_get(bakery_lock_t *bakery); 53634ec6c23b4037ec6efc2a594fd6b666339171e9Andrew Thoelkevoid bakery_lock_release(bakery_lock_t *bakery); 54634ec6c23b4037ec6efc2a594fd6b666339171e9Andrew Thoelkeint bakery_lock_try(bakery_lock_t *bakery); 554f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta 568c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew#else 578c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew 588c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathewtypedef struct bakery_info { 598c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew /* 608c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew * The lock_data is a bit-field of 2 members: 618c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew * Bit[0] : choosing. This field is set when the CPU is 628c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew * choosing its bakery number. 638c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew * Bits[1 - 15] : number. This is the bakery number allocated. 648c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew */ 658c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew volatile uint16_t lock_data; 668c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew} bakery_info_t; 678c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew 688c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathewvoid bakery_lock_get(unsigned int id, unsigned int offset); 698c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathewvoid bakery_lock_release(unsigned int id, unsigned int offset); 708c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew 718c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew#endif /* __USE_COHERENT_MEM__ */ 728c5fe0b5b9f1666b4ddd8f5849de80249cdebe40Soby Mathew#endif /* __ASSEMBLY__ */ 734f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a5Achin Gupta#endif /* __BAKERY_LOCK_H__ */ 74