From patchwork Thu May 26 10:30:05 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 820312 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p4QAUhsv008857 for ; Thu, 26 May 2011 10:30:43 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757344Ab1EZKak (ORCPT ); Thu, 26 May 2011 06:30:40 -0400 Received: from mail-ww0-f42.google.com ([74.125.82.42]:63526 "EHLO mail-ww0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757139Ab1EZKah (ORCPT ); Thu, 26 May 2011 06:30:37 -0400 Received: by wwk4 with SMTP id 4so4295136wwk.1 for ; Thu, 26 May 2011 03:30:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=C+6hgI+eGTk4TcOD6N04Mtl+WtQ1+69moC15KfKgdm8=; b=gCE8j+uHZBnRk5WBaxrYk+0Tm9h6IhIPU6V1NYMCm/XGoG6NdRShlC4nw1LxJPTSlU lU/K3Tu+gUkJtXvNkFyMjWo/m/d2x2VWf+PDq1WkZMv+SuedZ4W/eRm8MEp/hZdXIPde Nzx4bOStFcHZRnqLftMM+0VUC+4iTqw0qMBMk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=ojT0V2Wdvnl8U5WsQxRUR9Z4uNj6CNcvLwaOMd38+0Bl94mJgmAW4Q41jdWIv7nIj8 eOoIHMZSy/PGGOQgHc6Yyt27u/yf+D+sd7msEDwiINy2FQdmlUXKeRIyW5ZOisa4NR9T 6e3R2qfA4TaDV53qq0offcY2MAi+IDScDaMDI= Received: by 10.216.135.131 with SMTP id u3mr5595523wei.71.1306405836738; Thu, 26 May 2011 03:30:36 -0700 (PDT) Received: from localhost.localdomain ([109.66.201.145]) by mx.google.com with ESMTPS id et5sm365858wbb.33.2011.05.26.03.30.35 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 26 May 2011 03:30:36 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: john@jfloren.net, kvm@vger.kernel.org, mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, prasadjoshi124@gmail.com, Sasha Levin Subject: [PATCH v3 2/8] kvm tools: Add basic ioport dynamic allocation Date: Thu, 26 May 2011 13:30:05 +0300 Message-Id: <1306405811-4482-2-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.5.rc3 In-Reply-To: <1306405811-4482-1-git-send-email-levinsasha928@gmail.com> References: <1306405811-4482-1-git-send-email-levinsasha928@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Thu, 26 May 2011 10:30:44 +0000 (UTC) Add a very simple allocation of ioports. This prevents the need to coordinate ioports between different modules. Signed-off-by: Sasha Levin --- tools/kvm/include/kvm/ioport.h | 7 ++++++- tools/kvm/ioport.c | 24 +++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/tools/kvm/include/kvm/ioport.h b/tools/kvm/include/kvm/ioport.h index 67b4a6f..49f919f 100644 --- a/tools/kvm/include/kvm/ioport.h +++ b/tools/kvm/include/kvm/ioport.h @@ -9,6 +9,9 @@ /* some ports we reserve for own use */ #define IOPORT_DBG 0xe0 +#define IOPORT_START 0x6200 +#define IOPORT_SIZE 0x400 + #define IOPORT_VESA 0xa200 #define IOPORT_VESA_SIZE 256 #define IOPORT_VIRTIO_P9 0xb200 /* Virtio 9P device */ @@ -22,6 +25,8 @@ #define IOPORT_VIRTIO_RNG 0xf200 /* Virtio network device */ #define IOPORT_VIRTIO_RNG_SIZE 256 +#define IOPORT_EMPTY USHRT_MAX + struct kvm; struct ioport { @@ -37,7 +42,7 @@ struct ioport_operations { void ioport__setup_legacy(void); -void ioport__register(u16 port, struct ioport_operations *ops, int count, void *param); +u16 ioport__register(u16 port, struct ioport_operations *ops, int count, void *param); static inline u8 ioport__read8(u8 *data) { diff --git a/tools/kvm/ioport.c b/tools/kvm/ioport.c index 92ad152..492ce16 100644 --- a/tools/kvm/ioport.c +++ b/tools/kvm/ioport.c @@ -3,6 +3,9 @@ #include "kvm/kvm.h" #include "kvm/util.h" +#include "kvm/rbtree-interval.h" +#include "kvm/mutex.h" + #include /* for KVM_EXIT_* */ #include @@ -14,9 +17,23 @@ #define ioport_node(n) rb_entry(n, struct ioport, node) +static u16 free_io_port_idx; +DEFINE_MUTEX(free_io_port_idx_lock); static struct rb_root ioport_tree = RB_ROOT; bool ioport_debug; +static u16 ioport__find_free_port(void) +{ + u16 free_port; + + mutex_lock(&free_io_port_idx_lock); + free_port = IOPORT_START + free_io_port_idx * IOPORT_SIZE; + free_io_port_idx++; + mutex_unlock(&free_io_port_idx_lock); + + return free_port; +} + static struct ioport *ioport_search(struct rb_root *root, u64 addr) { struct rb_int_node *node; @@ -61,10 +78,13 @@ static struct ioport_operations dummy_write_only_ioport_ops = { .io_out = dummy_io_out, }; -void ioport__register(u16 port, struct ioport_operations *ops, int count, void *param) +u16 ioport__register(u16 port, struct ioport_operations *ops, int count, void *param) { struct ioport *entry; + if (port == IOPORT_EMPTY) + port = ioport__find_free_port(); + entry = ioport_search(&ioport_tree, port); if (entry) { pr_warning("ioport re-registered: %x", port); @@ -82,6 +102,8 @@ void ioport__register(u16 port, struct ioport_operations *ops, int count, void * }; ioport_insert(&ioport_tree, entry); + + return port; } static const char *to_direction(int direction)