<?xml version="1.0"?>
<!-- Copyright (C) 2022 Intel Corporation. -->
<!-- SPDX-License-Identifier: BSD-3-Clause -->
<xs:schema xml:id="root"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:acrn="https://projectacrn.org">

  <xs:assert test="every $cpu in vm//cpu_affinity//pcpu_id satisfies count(processors//thread[cpu_id = $cpu]) = 1">
    <xs:annotation acrn:severity="warning" acrn:report-on="$cpu">
      <xs:documentation>The physical CPU {$cpu} allocated to VM "{$cpu/ancestor::vm/name}" does not exist on the target board. Remove this CPU from the CPU affinity setting.</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $vm in /acrn-config/vm satisfies
                   count(distinct-values($vm//cpu_affinity//pcpu_id)) = count($vm//cpu_affinity//pcpu_id)">
    <xs:annotation acrn:severity="warning" acrn:report-on="$vm/cpu_affinity">
      <xs:documentation>VM "{$vm/name}" repeats a physical CPU affinity assignment: {$vm//cpu_affinity//pcpu_id}. Remove the duplicates.</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $pcpu in /acrn-config/vm[vm_type = 'RTVM']//cpu_affinity//pcpu_id satisfies
                   count(/acrn-config/vm[@id != $pcpu/ancestor::vm//companion_vmid ]//cpu_affinity[.//pcpu_id = $pcpu]) &lt;= 1">
    <xs:annotation acrn:severity="error" acrn:report-on="//vm//cpu_affinity[.//pcpu_id = $pcpu]">
      <xs:documentation>Because physical CPU {$pcpu} is assigned to a Real-time VM "{$pcpu/ancestor::vm/name}", it may not be shared with any other VM. Look for, and probably remove, duplicate affinity assignments to CPU {$pcpu} in these VM {//vm[cpu_affinity//pcpu_id = $pcpu]/name} settings.</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $pcpu in /acrn-config/vm[own_pcpu = 'y']//cpu_affinity//pcpu_id satisfies
                   count(/acrn-config/vm[@id != $pcpu/ancestor::vm//companion_vmid]//cpu_affinity[.//pcpu_id = $pcpu]) &lt;= 1">
    <xs:annotation acrn:severity="error" acrn:report-on="//vm//cpu_affinity[.//pcpu_id = $pcpu]">
      <xs:documentation>Physical CPU {$pcpu} is assigned to a VM "[{$pcpu/ancestor::vm/name}]" exclusively owns the physical CPUs assigned to it. Look for, and probably remove, any affinity assignments to CPU {$pcpu} in these VMs {//vm[cpu_affinity//pcpu_id = $pcpu and name != $pcpu/ancestor::vm/name]/name} settings.</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $vm in /acrn-config/vm[load_order != 'SERVICE_VM'] satisfies
                   count($vm/cpu_affinity/pcpu[pcpu_id != '']) > 0">
  <xs:annotation acrn:severity="error" acrn:report-on="$vm/cpu_affinity">
      <xs:documentation>Assign at least one physical CPU affinity (pCPU ID) for VM "{$vm/name}".</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $vm in /acrn-config/vm[load_order != 'SERVICE_VM'] satisfies
                   count(distinct-values(processors//thread[cpu_id = $vm//cpu_affinity//pcpu_id]/core_type)) &lt;= 1">
    <xs:annotation acrn:severity="error" acrn:report-on="$vm//cpu_affinity">
      <xs:documentation>The physical CPUs allocated to VM "{$vm/name}" have both performance cores: {processors//thread[cpu_id = $vm//cpu_affinity//pcpu_id and core_type = 'Core']/cpu_id} and efficiency cores: {processors//thread[cpu_id = $vm//cpu_affinity//pcpu_id and core_type = 'Atom']/cpu_id}, which is unsupported. Choose either all performance or all efficiency cores for CPU affinity.</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $vm in /acrn-config/vm[vm_type = 'RTVM'] satisfies
                   count($vm//pcpu_id[./text() = '0']) = 0">
    <xs:annotation acrn:severity="error" acrn:report-on="$vm//cpu_affinity">
      <xs:documentation>CPU 0 can not be assigned to Real-time VM "{$vm/name/text()}". Remove CPU 0 affinity setting for this VM.</xs:documentation>
    </xs:annotation>
  </xs:assert>

</xs:schema>
