/*-1* Copyright (c) 2014 The FreeBSD Foundation2*3* This software was developed by Semihalf under4* the sponsorship of the FreeBSD Foundation.5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/param.h>2829#include <machine/armreg.h>30#include <machine/atomic.h>3132#include <stand.h>3334#include "bootstrap.h"35#include "cache.h"3637static long cache_flags;38#define CACHE_FLAG_DIC_OFF (1<<0)39#define CACHE_FLAG_IDC_OFF (1<<1)4041static bool42get_cache_dic(uint64_t ctr)43{44if ((cache_flags & CACHE_FLAG_DIC_OFF) != 0) {45return (false);46}4748return (CTR_DIC_VAL(ctr) != 0);49}5051static bool52get_cache_idc(uint64_t ctr)53{54if ((cache_flags & CACHE_FLAG_IDC_OFF) != 0) {55return (false);56}5758return (CTR_IDC_VAL(ctr) != 0);59}6061static unsigned int62get_dcache_line_size(uint64_t ctr)63{64unsigned int dcl_size;6566/*67* Relevant field [19:16] is LOG268* of the number of words in DCache line69*/70dcl_size = CTR_DLINE_SIZE(ctr);7172/* Size of word shifted by cache line size */73return (sizeof(int) << dcl_size);74}7576void77cpu_flush_dcache(const void *ptr, size_t len)78{79uint64_t cl_size, ctr;80vm_offset_t addr, end;8182/* Accessible from all security levels */83ctr = READ_SPECIALREG(ctr_el0);8485if (get_cache_idc(ctr)) {86dsb(ishst);87} else {88cl_size = get_dcache_line_size(ctr);8990/* Calculate end address to clean */91end = (vm_offset_t)ptr + (vm_offset_t)len;92/* Align start address to cache line */93addr = (vm_offset_t)ptr;94addr = rounddown2(addr, cl_size);9596for (; addr < end; addr += cl_size)97__asm __volatile("dc civac, %0" : : "r" (addr) :98"memory");99/* Full system DSB */100dsb(ish);101}102}103104void105cpu_inval_icache(void)106{107uint64_t ctr;108109/* Accessible from all security levels */110ctr = READ_SPECIALREG(ctr_el0);111112if (get_cache_dic(ctr)) {113isb();114} else {115__asm __volatile(116"ic ialluis \n"117"dsb ish \n"118"isb \n"119: : : "memory");120}121}122123static int124command_cache_flags(int argc, char *argv[])125{126char *cp;127long new_flags;128129if (argc == 3) {130if (strcmp(argv[1], "set") == 0) {131new_flags = strtol(argv[2], &cp, 0);132if (cp[0] != '\0') {133printf("Invalid flags\n");134} else {135printf("Setting cache flags to %#lx\n",136new_flags);137cache_flags = new_flags;138return (CMD_OK);139}140}141}142143printf("usage: cache_flags set <value>\n");144return (CMD_ERROR);145}146COMMAND_SET(cache_flags, "cache_flags", "Set cache flags", command_cache_flags);147148149