Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 19 | #include "hf/addr.h" |
| 20 | #include "hf/vm.h" |
| 21 | |
| 22 | #include "vmapi/hf/spci.h" |
| 23 | |
Jose Marinho | fc0b2b6 | 2019-06-06 11:18:45 +0100 | [diff] [blame] | 24 | #define SPCI_VERSION_MAJOR 0x0 |
| 25 | #define SPCI_VERSION_MINOR 0x9 |
| 26 | |
| 27 | #define SPCI_VERSION_MAJOR_OFFSET 16 |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 28 | |
| 29 | struct spci_mem_transitions { |
Andrew Walbran | 1281ed4 | 2019-10-22 17:23:40 +0100 | [diff] [blame] | 30 | uint32_t orig_from_mode; |
| 31 | uint32_t orig_to_mode; |
| 32 | uint32_t from_mode; |
| 33 | uint32_t to_mode; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 36 | /* TODO: Add device attributes: GRE, cacheability, shareability. */ |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 37 | static inline uint32_t spci_memory_attrs_to_mode(uint16_t memory_attributes) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 38 | { |
| 39 | uint32_t mode = 0; |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 40 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 41 | switch (spci_get_memory_access_attr(memory_attributes)) { |
| 42 | case SPCI_MEMORY_RO_NX: |
| 43 | mode = MM_MODE_R; |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 44 | break; |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 45 | case SPCI_MEMORY_RO_X: |
| 46 | mode = MM_MODE_R | MM_MODE_X; |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 47 | break; |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 48 | case SPCI_MEMORY_RW_NX: |
| 49 | mode = MM_MODE_R | MM_MODE_W; |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 50 | break; |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 51 | case SPCI_MEMORY_RW_X: |
| 52 | mode = MM_MODE_R | MM_MODE_W | MM_MODE_X; |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 53 | break; |
| 54 | } |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 55 | |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 56 | return mode; |
| 57 | } |
| 58 | |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 59 | static inline struct spci_value spci_error(uint64_t error_code) |
| 60 | { |
Andrew Walbran | 9b19b75 | 2019-10-10 13:49:25 +0100 | [diff] [blame] | 61 | return (struct spci_value){.func = SPCI_ERROR_32, .arg2 = error_code}; |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 64 | struct spci_value spci_msg_handle_architected_message( |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 65 | struct vm_locked to_locked, struct vm_locked from_locked, |
Andrew Walbran | 85aabe9 | 2019-12-03 12:03:03 +0000 | [diff] [blame] | 66 | struct spci_memory_region *memory_region, uint32_t size, |
| 67 | uint32_t attributes, struct mpool *api_page_pool); |